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

Python3 处理中文问题

  •  
  •   JasperYanky · Apr 19, 2017 · 3434 views
    This topic created in 3445 days ago, the information mentioned may be changed or developed.
    requests 解析 json 数据,在终端可以执行,但是在后台运行报错,如下:

    'ascii' codec can't encode characters in position 530-531: ordinal not in range(128)

    json 中有中文 应该是这个引起的。
    json() 之前 encode('utf-8')也没法解决

    这种情况下怎么解决?
    Supplement 1  ·  Apr 19, 2017
    最后 在脚本里面输出了环境变量解决了
    export PYTHONIOENCODING=UTF-8
    8 replies  •  2017-04-19 23:51:16 +08:00
    clino
        1
    clino  
       Apr 19, 2017   ❤️ 2
    那你要先搞清楚字符串到底是什么编码
    Feva
        2
    Feva  
       Apr 19, 2017
    # coding=utf-8
    or
    # -*- coding:utf-8 -*-
    后台文件有么?
    JasperYanky
        3
    JasperYanky  
    OP
       Apr 19, 2017
    @Feva 脚本之前是 ok 的 现在 json 中的数据变了 应该是某个中文字符引起的
    JasperYanky
        4
    JasperYanky  
    OP
       Apr 19, 2017
    @clino Python3 默认是 unicode 吧
    Feva
        5
    Feva  
       Apr 19, 2017   ❤️ 1
    @JasperYanky 你这个明显是处理非 assii 字符报错
    py 文件头加编码不行,再加入如下代码
    import sys
    reload(sys)
    sys.setdefaultencoding('utf-8')
    ipwx
        6
    ipwx  
       Apr 19, 2017
    Python 脚本没有指定编码,所以解释器就报错了吧,应该不是 Json 的锅。
    ipwx
        7
    ipwx  
       Apr 19, 2017
    $ ipython
    Python 3.6.0 |Anaconda 4.3.1 (x86_64)| (default, Dec 23 2016, 13:19:00)
    Type "copyright", "credits" or "license" for more information.

    IPython 5.1.0 -- An enhanced Interactive Python.
    ? -> Introduction and overview of IPython's features.
    %quickref -> Quick reference.
    help -> Python's own help system.
    object? -> Details about 'object', use 'object??' for extra details.

    In [1]: import json

    In [2]: json.dumps('你好')
    Out[2]: '"\\u4f60\\u597d"'
    yucongo
        8
    yucongo  
       Apr 19, 2017
    可以是国人的网页没跟足标准, 试试 resp.encoding='utf-8':
    url = '...'
    resp = requests.get(url)
    resp.encoding = 'UTF-8'

    =====
    In [189]: url = 'http://www.baidu.com'

    In [190]: import requests

    In [191]: resp = requests.get(url)

    In [192]: resp.encoding
    Out[192]: 'ISO-8859-1' # bad !

    In [200]: chardet.detect(resp.content)
    Out[200]: {'confidence': 0.99, 'encoding': 'utf-8'}

    In [201]: resp.encoding = 'utf-8'
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2415 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 05:02 · PVG 13:02 · LAX 22:02 · JFK 01:02
    ♥ Do have faith in what you're doing.