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

求助:字符串终端打印正常,压到数组中去就变编码了怎么办?

  •  
  •   justfly · 2013-01-25 18:03:57 +08:00 · 4014 次点击
    这是一个创建于 4109 天前的主题,其中的信息可能已经有所发展或是发生改变。
    谢了个爬虫爬在线字典,上面的子print就好好的,append数组中去就变成下面这样了。我知道是编码解码的问题,可是不知道怎么让他显示正常,求助!

    (小)汽车,轿车
    车辆;有轨电车
    [英国方言]任何有轮的车(如运货马车、农用马车)
    [铁路] (火车的)车厢,车辆
    [美国英语](电梯的)升降室
    (气球等的)吊舱,吊篮
    [诗歌用语](古代的)战车,凯旋车

    [u'(\u5c0f)\u6c7d\u8f66\uff0c\u8f7f\u8f66', u'\u8f66\u8f86\uff1b\u6709\u8f68\u7535\u8f66', u'[\u82f1\u56fd\u65b9\u8a00]\u4efb\u4f55\u6709\u8f6e\u7684\u8f66(\u5982\u8fd0\u8d27\u9a6c\u8f66\u3001\u519c\u7528\u9a6c\u8f66)', u'\u3010\u94c1\u8def\u3011(\u706b\u8f66\u7684)\u8f66\u53a2\uff0c\u8f66\u8f86', u'[\u7f8e\u56fd\u82f1\u8bed](\u7535\u68af\u7684)\u5347\u964d\u5ba4', u'(\u6c14\u7403\u7b49\u7684)\u540a\u8231\uff0c\u540a\u7bee', u'[\u8bd7\u6b4c\u7528\u8bed](\u53e4\u4ee3\u7684)\u6218\u8f66\uff0c\u51ef\u65cb\u8f66']
    8 条回复    1970-01-01 08:00:00 +08:00
    zxc111
        1
    zxc111  
       2013-01-25 18:33:54 +08:00
    append时先decode('gb2312')
    输出时encode回来就好了
    reusFork
        2
    reusFork  
       2013-01-25 19:02:09 +08:00
    自己写个打印函数就行,例如
    http://gist.github.com/4633523
    justfly
        3
    justfly  
    OP
       2013-01-25 19:36:16 +08:00
    @zxc111 没用 UnicodeEncodeError: 'ascii' codec can't encode character u'\u87d2' in position 0: ordinal not in range(128)
    justfly
        4
    justfly  
    OP
       2013-01-25 19:37:45 +08:00
    @reusFork 我要存到数组里面 然后存到mongodb 不是为了打印出来
    013231
        5
    013231  
       2013-01-25 20:33:47 +08:00
    這跟編碼沒關係, 存入list不會轉換unicode的編碼 -- 事實上根本不用考慮unicode的編碼, 只有str存在編碼問題.
    你直接存進mongodb就行了.
    關於python中文字編碼問題請看這個帖子: http://www.v2ex.com/t/35110
    justfly
        6
    justfly  
    OP
       2013-01-25 20:43:09 +08:00
    @013231 我要先写入文件,可是json.dumps(我构造的dict)之后,写入文件也是这些东西啊,我就想让他在文件里显示出中文,怎么就这么难。一堆

    {"gloss": ["\u6c7d\u8f66\uff0c\u8f7f\u8f66", "\u540a\u8231\uff0c\u98de\u8239\u540a\u8231", "\u8f66\u53a2\uff0c\u706b\u8f66\u8f66\u53a2", "\u6709\u8f68\u7535\u8f66\uff0c\u7535\u8f66", "\u5347\u964d\u53a2\uff0c\u7535\u68af", "\u6218\u8f66", "\u8f66\uff0c\u8f66\u8f86\uff0c\u6709\u8f6e\u7684\u8f66", "\u8fd0\u8d27\u9a6c\u8f66", "\u51ef\u65cb\u95e8", "\u540a\u7bee", "\u8f66\u76ae", "\u6c14\u7403", "\u5ba2\u8f66"], "word": "python"}
    wwqgtxx
        7
    wwqgtxx  
       2013-01-25 21:06:06 +08:00 via Android
    用python3.3试试,可能会解决你的问题
    013231
        8
    013231  
       2013-01-25 21:25:50 +08:00
    @justfly 首先, json中使用轉義寫法是完全符合規範的(http://www.ietf.org/rfc/rfc4627.txt). 如果你想以不轉義的方式寫入json, 可以這樣:
    http://gist.github.com/4634405
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2881 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 14:23 · PVG 22:23 · LAX 07:23 · JFK 10:23
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.