37,719
社区成员
发帖
与我相关
我的任务
分享
#coding="utf-8"
import sys
import MySQLdb
import types
import codecs
conn = MySQLdb.connect(host='...', user='...', passwd='...', db='...', port=8001, charset='gbk')
cursor = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor)
n = cursor.execute("select * from t_award_info limit 2");
fAllData = cursor.fetchall();
print "fAllData:\n" + str(fAllData);
for row in fAllData:
for r in row:
k = r.encode("gbk")
print k
print k + " = " + (str(row[k]).encode("gbk") if (row[k] is types.StringType) else str(row[k]).encode("gbk"))
cursor.close();
conn.close();
print k + " = " + (str(row[k]).encode("gbk") if (row[k] is types.StringType) else str(row[k]).encode("gbk"))
#... now you have k, row[k]
try:
k += str(row[k])
except UnicodeEncodeError:
k += row[k]
中文注释的问题,#5楼就提到了。
for row in fAllData:
for r in row:
k = r.encode("gbk")
temp1 = row[k]
temp2 = None
if(type(temp1) is types.StringType): # is string, translate coding
temp2 = temp1.encode("gbk") # translate coding here
else:
# rase exception : UnicodeEncodeError:
#'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)
temp2 = str(row[k]) # not a string, then convert it 注意这是里报错
print temp2;
for row in fAllData:
for r in row:
k = r.encode("gbk")
temp1 = row[k]
temp2 = None
if(type(temp1) == unicode): # is string, translate coding 这里直接报错:NameError: Name 'unicode' is not defined
temp2 = temp1.encode("gbk") # translate coding here
else:
# rase exception : UnicodeEncodeError:
#'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)
temp2 = str(row[k]) # not a string, then convert it
print temp2;
#coding="utf-8"
import sys
import MySQLdb
#import codecs
#import types
import chardet # 需要使用chardet来识别每列的编码.
conn = MySQLdb.connect(host='...', user='...', passwd='...', db='...', port=8001, charset='gbk')
cursor = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor)
n = cursor.execute("select * from t_award_info limit 2");
fAllData = cursor.fetchall();
print "fAllData:\n" + str(fAllData);
for row in fAllData:
for r in row:
k = r.encode("gbk")
print k
column_encode = chardet.detect(row[k])['encoding'] #先获得编码
print k + " = " + row[k].decode(column_encode).encode('gbk') # 再编码成gbk
cursor.close();
conn.close();
因为一直报,ascii转码不成,str(row[k])的前3个字节(position 0-2)转不成; 所以我认为是MySQLdb出来的字符编码为:ascii了; 但在连接时我就指定为:gbk了; 结果还是老报ascii的问题; 之后,你在5L说了:先把源来的编码(from coding)转成unicode,再encode自己想要的编码 因为decode,与encode这两个方法一般的对象是没有的(除非自己的类自己写的不算); decoder("from coding").encode("target coding"); 对了,经过上面的文字分析; str(row[k])一报报转ascii失败; 那说明str默认是转成:ascii码的? 那python有没有,将任意对象,转成指自己编码字符串的方法; 我现在知道:class 下都可以自己写个:__str__来给外部str(myClassInst)来调用; 但转出来的字符默认都是ascii; 有没有类似这样的方法,假设:str(obj, "charset")
print k + "=" row[k]
或者转为unicode:
print k + "=" row[k].decode('gbk')
问题是这个"from coding"我不知道;
In [78]: a= u'汉字'.encode('utf-8')
In [79]: len(a)
Out[79]: 6
In [80]: a.decode('gbk')
Out[80]: u'\u59f9\u590a\u74e7'
In [81]: print a.decode('gbk')
姹夊瓧
如果是第二种情况,设法找出你的数据库用的编码。