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
bestehen
V2EX  ›  Python

python3 如何把字典的 unicode 显示汉字

  •  
  •   bestehen · 2018-08-03 09:49:42 +08:00 · 5735 次点击
    这是一个创建于 2086 天前的主题,其中的信息可能已经有所发展或是发生改变。
    Traceback (most recent call last):
    {"person_news":json.dumps(OrderedDict(sorted(person_news.items())),encoding="Utf-8",ensure_ascii=False)}])
    File "/usr/lib64/python3.4/json/__init__.py", line 237, in dumps
    **kw).encode(obj)
    TypeError: __init__() got an unexpected keyword argument 'encoding'


    这种格式好像是 python2 的,报错不知道如何处理,求教!
    9 条回复    2018-08-03 14:59:36 +08:00
    chenxytw
        1
    chenxytw  
       2018-08-03 09:57:07 +08:00
    报错就是没有 encoding 这个参数啊.......
    bestehen
        2
    bestehen  
    OP
       2018-08-03 10:14:11 +08:00
    @chenxytw 我知道这个参数啊,。我不明白啊
    bestehen
        3
    bestehen  
    OP
       2018-08-03 10:27:35 +08:00
    @chenxytw 我不是找人给我翻译啊
    Yuchen
        4
    Yuchen  
       2018-08-03 10:27:48 +08:00   ❤️ 1
    ```
    json.dumps(['汉字'], ensure_ascii=False)
    ```
    bestehen
        5
    bestehen  
    OP
       2018-08-03 10:38:59 +08:00
    我那个是字典格式,你这里外面还嵌套一个列表?
    Yuchen
        6
    Yuchen  
       2018-08-03 11:07:29 +08:00
    json.dumps 方法没有 encoding 参数,你直接去掉就好了。。
    Sylv
        7
    Sylv  
       2018-08-03 11:48:30 +08:00 via iPhone
    要学会去看文档:
    json.dumps(obj, *, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw)


    Python 3 的 json.dumps 返回的是 str 类型的 Unicode 字符串,所以不像 Python 2 需要 encoding 参数。
    PulpFunction
        8
    PulpFunction  
       2018-08-03 13:08:29 +08:00
    utf8 编码啊
    jayx
        9
    jayx  
       2018-08-03 14:59:36 +08:00
    把系统环境默的认编码设置为 utf-8
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2304 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 00:45 · PVG 08:45 · LAX 17:45 · JFK 20:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.