
" title="source: imgur.com" /></a> 1 Yinz 2017 年 1 月 7 日 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 所以没有自动编码 |
2 popbones 2017 年 1 月 7 日 via iPad 这个不是编码问题,只是 print 打印嵌在其它结构里的字符串会默认 escape 模式。 shell 的话一般直接打印字符串本身就没事了,如 print result[0]。 |
3 qile1 2017 年 1 月 8 日 via Android 解决了吗?我访问 mssql 也是,不给如果显示我转码 lantin ,然后 utf-8 解码 |
4 q397064399 2017 年 1 月 8 日 用 python3 吧 |
5 ericbize OP |
8 ericbize OP @q397064399 找不到解决方法的时候我也是这样想的,可我只是写一部分,并不能吧别人的部分也让用 3 ~~~ |
9 q397064399 2017 年 1 月 8 日 |
10 qile1 2017 年 1 月 8 日 via Android @ericbize 抱歉,手机打字好几个错别字。 我使用的是 pymssql 链接的 mssql 数据库,也有乱码。 网上建议是执行的时候先 utf-8 编码下,查询到结果首先编码为 utf-8 ,数据库链接设置编码格式为 utf-8 你试试转码后是否显示乱码。 d=result[0][0].encode(“ latin-1 ”).decode(“ gbk ”) 试试 print d |
11 ericbize OP @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) 数字,英文都可以,中文不行~~~ |
12 ericbize OP @q397064399 我也感觉不兼容各种坑~~~ , 但是有时候升级也是变革嘛,就像 intel 新款的 cpu 也不能装 xp |
13 louzhumuyou 2017 年 1 月 8 日 @ericbize 我是用 json.dumps 和 json.loads() |