V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
smdbh
V2EX  ›  Python

有什么优雅的方法把正则解析后的文本变为其他数据类型?

  •  
  •   smdbh · 2018-05-10 00:03:22 +08:00 · 2497 次点击
    这是一个创建于 2150 天前的主题,其中的信息可能已经有所发展或是发生改变。
    例如经过正则匹配后,用 dictgroup 得到一个 dict 数据结构,其中的 key 和 val 都是文本,但实际某些 key 需要的是 int 或是 float 的数据类型。除了把他们读出来手动改完写回去,还有其他方法么
    5 条回复    2018-05-10 14:31:08 +08:00
    crb912
        1
    crb912  
       2018-05-10 00:57:20 +08:00 via Android
    从正则的结果中取出你要的 key,假设是 temp,强制转换一下不可以么? int(temp)。
    python 中的 int 是类,允许从字符串转到 int,不同于 c/cpp
    Leigg
        2
    Leigg  
       2018-05-10 08:28:53 +08:00 via Android
    python 中的 map 函数了解一下
    awakn123
        3
    awakn123  
       2018-05-10 09:58:12 +08:00
    都数据类型了,不说什么语言真的好么
    xxxy
        4
    xxxy  
       2018-05-10 10:17:10 +08:00
    python 中的列表推导
    jmc891205
        5
    jmc891205  
       2018-05-10 14:31:08 +08:00
    一些 key 需要是 int 或 float 另一些保持文本?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3352 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 00:05 · PVG 08:05 · LAX 17:05 · JFK 20:05
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.