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

Python3 中字典转为字符串转义问题

  •  
  •   Hiyokunotori · Dec 29, 2020 · 2596 views
    This topic created in 2102 days ago, the information mentioned may be changed or developed.

    我有一个字典 A,其中会有一些转义符号

    dict_a = {xxx}

    这个 res1 是我期望的结果

    res1 = r'{xxx}'

    这个 res2 不是我期望的结果,和 res1 并不一致

    res2 = str(dict_a)

    请问我如何使用函数的方式得到 ditc_a 的 res1 的结果呢

    例如 res1 = 某个函数(dict_a)?

    2 replies  •  2020-12-29 15:14:33 +08:00
    chenstack
        1
    chenstack  
       Dec 29, 2020
    应该给个例子,可以先试试 repr(dict_a)是不是你想要的
    loliordie
        2
    loliordie  
       Dec 29, 2020 via Android   ❤️ 1
    你说的东西 叫做 json

    这个过程你怎么保证所有 key 和 value 是 serializable 的? 先考虑好这个

    如果没有这个痛点 直接 json.dumps(dict)即可
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2344 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 11:00 · PVG 19:00 · LAX 04:00 · JFK 07:00
    ♥ Do have faith in what you're doing.