想请教怎么转 码 - 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
ericbize
V2EX    Python

想请教怎么转 码

  •  
  •   ericbize 2017-01-07 20:37:54 +08:00 3289 次点击
    这是一个创建于 3265 天前的主题,其中的信息可能已经有所发展或是发生改变。
    #-*-coding:utf-8-*-
    import MySQLdb
    import sys
    import os

    reload(sys)
    sys.setdefaultencoding('utf8')

    cOnn= MySQLdb.connect(
    host='.gz.cdb.myqcloud.com',
    port = ,
    user='',
    passwd='',
    db ='robot',
    charset="utf8"
    )
    cur = conn.cursor()

    entering = raw_input("please enter : ")

    entering="'" + entering + "'"

    sql = " SELECT REPLY FROM robot.reply where RECEIVE = "

    cur.execute(sql+entering)
    results = cur.fetchone()

    print results

    conn.close()


    写了一段 py , 按照数据库有的内容输出,可是中文输出的是 unicode 请问怎么转码


    <a href="http://imgur.com/jxAdVsS"><img src="" title="source: imgur.com" /></a>
    13 条回复    2017-01-08 21:41:05 +08:00
    Yinz
        1
    Yinz  
       2017-01-07 21:01:19 +08:00
    cur.execute(sql+entering)
    results = cur.fetchone()

    print results
    改成
    cur.execute(sql+entering)
    results = cur.fetchone()

    for result in results:
    print result

    Python print 的时候会自动将 unicode str 自动编码为合适的编码。你一开始 print 的是个 tuple 所以没有自动编码
    popbones
        2
    popbones  
       2017-01-07 21:02:34 +08:00 via iPad
    这个不是编码问题,只是 print 打印嵌在其它结构里的字符串会默认 escape 模式。 shell 的话一般直接打印字符串本身就没事了,如 print result[0]。
    qile1
        3
    qile1  
       2017-01-08 00:24:25 +08:00 via Android
    解决了吗?我访问 mssql 也是,不给如果显示我转码 lantin ,然后 utf-8 解码
    q397064399
        4
    q397064399  
       2017-01-08 07:54:15 +08:00
    用 python3 吧
    ericbize
        5
    ericbize  
    OP
       2017-01-08 09:56:37 +08:00
    @Yinz 感谢,解决了 print 中文的话还要加一个 str
    for result in results:
    print str(result)
    ericbize
        6
    ericbize  
    OP
       2017-01-08 09:56:50 +08:00
    @popbones 谢谢指教,你的方法也可以。
    ericbize
        7
    ericbize  
    OP
       2017-01-08 09:57:02 +08:00
    @qile1 mysql 吗? 上面的程序可以解决,数字,中英文输入输出
    ericbize
        8
    ericbize  
    OP
       2017-01-08 09:57:07 +08:00
    @q397064399 找不到解决方法的时候我也是这样想的,可我只是写一部分,并不能吧别人的部分也让用 3 ~~~
    q397064399
        9
    q397064399  
       2017-01-08 10:55:21 +08:00
    @ericbize 所以这就是 python 的悲剧之处,

    缺乏静态类型检查 本来就不特别适合协作开发
    结果还搞了个 python2.7 python3 不兼容
    qile1
        10
    qile1  
       2017-01-08 11:21:05 +08:00 via Android
    @ericbize
    抱歉,手机打字好几个错别字。
    我使用的是 pymssql 链接的 mssql 数据库,也有乱码。
    网上建议是执行的时候先 utf-8 编码下,查询到结果首先编码为 utf-8 ,数据库链接设置编码格式为 utf-8

    你试试转码后是否显示乱码。
    d=result[0][0].encode(“ latin-1 ”).decode(“ gbk ”)
    试试 print d
    ericbize
        11
    ericbize  
    OP
       2017-01-08 15:22:59 +08:00
    @qile1 SyntaxError: invalid syntax
    ubuntu@VM-18-113-ubuntu:~$ python /home/ubuntu/fetch3.py
    please enter : hi
    h
    ubuntu@VM-18-113-ubuntu:~$ python /home/ubuntu/fetch3.py
    please enter : 吃饭
    Traceback (most recent call last):
    File "/home/ubuntu/fetch3.py", line 29, in <module>
    d=results[0][0].encode("latin-1").decode("gbk")
    UnicodeEncodeError: 'latin-1' codec can't encode character u'\u997f' in position 0: ordinal not in range(256)
    ubuntu@VM-18-113-ubuntu:~$ python /home/ubuntu/fetch3.py
    please enter : hi
    h
    ubuntu@VM-18-113-ubuntu:~$ python /home/ubuntu/fetch3.py
    please enter : 吃饭
    Traceback (most recent call last):
    File "/home/ubuntu/fetch3.py", line 29, in <module>
    d=results[0][0].encode("latin-1").decode("gbk")
    UnicodeEncodeError: 'latin-1' codec can't encode character u'\u997f' in position 0: ordinal not in range(256)



    数字,英文都可以,中文不行~~~
    ericbize
        12
    ericbize  
    OP
       2017-01-08 15:24:16 +08:00
    @q397064399 我也感觉不兼容各种坑~~~ , 但是有时候升级也是变革嘛,就像 intel 新款的 cpu 也不能装 xp
    louzhumuyou
        13
    louzhumuyou  
       2017-01-08 21:41:05 +08:00
    @ericbize 我是用 json.dumps 和 json.loads()
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5228 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 27ms UTC 08:30 PVG 16:30 LAX 00:30 JFK 03:30
    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