是字符集问题么?如何解决?

tim_spac 2008-01-21 05:36:39
这是什么问题?
Traceback (most recent call last):
File "D:\Workspace\Python\conn2sugar.py", line 37, in
print account[1]
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-13: ordinal not in range(128)
源码如下
#!Python
# -*-coding:GBK -*-

import MySQLdb

accounts = {}
conn = MySQLdb.connect (
host="sugarhost",
user="sugardbuser",
passwd="sugardbuserpwd",
db="sugarcrm",
charset="GBK")
cursor = conn.cursor ()
cursor.execute ('''
select
account_customercode_c as code
,name
,conf_dep_c as dept
,conf_zone_c as zone
,conf_assigned_user_c as sale
from accounts a
join accounts_cstm a0 on a.id=a0.id_c
where account_customercode_c regexp '[[:digit:]]{6}';
''')
for row in cursor.fetchall():
accounts[row[0]] = {
'Name':row[1],
'Dept':row[2],
'Zone':row[3],
'Sale':row[4]}
cursor.close ()
conn.close ()

for code,account in accounts.items():
print 'Account:[%s]%s' % (code,account['Name'])

如何解决?
...全文
417 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
omencathay 2009-01-14
  • 打赏
  • 举报
回复
20.4.13 Can't initialize character set xxx error.
If you get an error like:

MySQL Connection Failed: Can't initialize character set xxx

This means one of the following things:

The character set is a multi-byte character set and you have not support for the character set in the client. In this case you need to recompile the client with --with-charset=xxx or with --with-extra-charsets=xxx. See section 4.7.3 Typical configure Options. All standard MySQL binaries are compiled with --with-extra-character-sets=complex which will enable support for all multi-byte character sets. See section 10.1.1 The Character Set Used for Data and Sorting.
The character set is a simple character set which is not compiled into mysqld and the character set definition files is not in the place where the client expect to find them. In this case you need to:
Recompile the client with support for the character set. See section 4.7.3 Typical configure Options.
Specify to the client where the character set definition files are. For many client you can do this with the --character-sets-dir=path-to-charset-dir option.
Copy the character definition files to the path where the client expect them to be.
tim_spac 2008-01-22
  • 打赏
  • 举报
回复
用:
for code,account in accounts.items():
print account
得到如下结果:

{'Dept': None, 'Name': u'\u5317\u4eac\u5feb\u5229\u670b\u4fe1\u606f\u54a8\u8be2\u6709\u9650\u516c\u53f8
', 'Zone': None, 'Sale': u''}
{'Dept': u'', 'Name': u'\u5317\u4eac\u76db\u4e16\u94f6\u76fe\u6587\u5316\u53d1\u5c55\u6709\u9650\u516c\u53f8', 'Zone': u
'', 'Sale': u'N/A'}
{'Dept': None, 'Name': u'\u5317\u4eac\u53cc\u5b50\u4fe1\u606f\u54a8\u8be2\u6709\u9650\u516c\u53f8
', 'Zone': None, 'Sale': None}
{'Dept': u'', 'Name': u'\u5317\u4eac\u521b\u60f3\u7a7a\u95f4\u5546\u52a1\u901a\u4fe1\u670d\u52a1\u6709\u9650\u516c\u53f8
', 'Zone': u'north', 'Sale': u'//'}
{'Dept': u'', 'Name': u'\u5317\u4eac\u6602\u901a\u79d1\u6280\u5f00\u53d1\u6709\u9650\u516c\u53f8', 'Zone': u'', 'Sale':
u'\u65e0\u6548'}

用:
for code,account in accounts.items():
print account['Name']
正常输出中文;

再恢复
print 'Account:[%s]%s' % (code,account['Name'])
也能正常输出了。
?! 0_0 !!!!

为什么? 没道理啊!
Semigod 2008-01-22
  • 打赏
  • 举报
回复
首先,你应该到SQL里去看你的数据在那里是以什么形式存储的,如果不是UNICODE方式存储的的话,那这里你的代码就要改一改,否则的话,用GBK不应该出问题

for code,account in accounts.items():
print 'Account:[%s]%s' % (code, account['Name'].decode("gbk"))
tim_spac 2008-01-22
  • 打赏
  • 举报
回复
谢谢各位先。
sorry, 消息应该是:
Traceback (most recent call last):
File "D:\Workspace\Python\conn2sugar.py", line 35, in
print 'Account:[%s]%s' % (code,account['Name'])
UnicodeEncodeError: 'ascii' codec can't encode characters in position 16-29: ordinal not in range(128)

既: print 'Account:[%s]%s' % (code,account['Name'])


另:
1) 若把 # -*-coding:GBK -*- 处变更为 # -*-coding:UTF-8 -*-, 则消息相同
2) 若把 conn = MySQLdb.connect (..,charset="GBK")出的GBK变更为 UTF-8,则消息如下:

Traceback (most recent call last):
File "D:\Workspace\Python\test.py", line 12, in
charset="UTF-8")
File "C:\Python25\Lib\site-packages\MySQLdb\__init__.py", line 74, in Connect
return Connection(*args, **kwargs)
File "C:\Python25\Lib\site-packages\MySQLdb\connections.py", line 198, in __init__
self.set_character_set(charset)
File "C:\Python25\Lib\site-packages\MySQLdb\connections.py", line 277, in set_character_set
super(Connection, self).set_character_set(charset)
_mysql_exceptions.OperationalError: (2019, "Can't initialize character set UTF-8 (path: C:\\mysql\\\\share\\charsets\\)")
sh_royan 2008-01-22
  • 打赏
  • 举报
回复
应该是你读出来了中文,解析不了
GBK改成UTF-8 ?
或者解析(print)的时候处理下?
Semigod 2008-01-21
  • 打赏
  • 举报
回复
貌似你少COPY代码过来SHOW了
iambic 2008-01-21
  • 打赏
  • 举报
回复
conn2sugar.py
line 37
print account[1]
是哪一行?

37,721

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧