推荐学习书目
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
bestehen
V2EX  ›  Python

Python 里面把字符串变字典,因为经常在抓数据的时候遇到

  •  
  •   bestehen · Jul 27, 2018 · 2831 views
    This topic created in 2873 days ago, the information mentioned may be changed or developed.

    除了用 json.loads 之外 看到网上有人说 eval 但是用的时候总是报错各种 Traceback (most recent call last): File "run.py", line 44, in <module> run().get_spider(sys.argv[1:][0]) File "run.py", line 29, in get_spider news_source().get_odaily_content() File "/home/shenjianlin/my_project/news_source/news_source.py", line 544, in get_odaily_content print(eval(response.text)) File "<string>", line 1, in <module> NameError: name 'null' is not defined 用了就报错!</module></string></module>

    5 replies    2018-07-29 19:15:41 +08:00
    Atang
        1
    Atang  
       Jul 27, 2018
    建议代码格式整理一下。。不然。。。
    rocketman13
        2
    rocketman13  
       Jul 28, 2018
    eval 可以将字符串改为字典?
    noqwerty
        3
    noqwerty  
       Jul 29, 2018
    可以用 from ast import literal_eval,不过有什么特别的原因不用 json.loads 吗?

    另外你这个报错看着像是返回值并不是 json 格式,只是个 null。
    bestehen
        4
    bestehen  
    OP
       Jul 29, 2018
    @noqwerty 我是看看有没有其他的处理方法
    sxd96
        5
    sxd96  
       Jul 29, 2018
    eval 还有安全性问题,是什么原因不能用 json.loads 吗?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3112 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 47ms · UTC 13:44 · PVG 21:44 · LAX 06:44 · JFK 09:44
    ♥ Do have faith in what you're doing.