求助:字符串终端打印正常,压到数组中去就变编码了怎么办? - V2EX
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 4527 次点击
    这是一个创建于 4712 天前的主题,其中的信息可能已经有所发展或是发生改变。
    谢了个爬虫爬在线字典,上面的子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('')
    输出时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     Solana     2113 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 28ms UTC 16:10 PVG 00:10 LAX 08:10 JFK 11:10
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86