在我的一个python程序(python 2.7)中,我需要处理一些汉字:


我有一个文件A.txt,它有两列:“名称”和“分数”,“名称”列可以用一些中文字符串值,并且score是1到10之间的整数值。A.txt被编码GBK中的中文字符编码。
我将A.txt的每一行插入到我的mysql表tb_name_score中,它具有三列:ID,NAME,SCORE,其NAME列的编码为latin1_swedish_ci
现在,我有另一个文件名B.txt,它也有两列,即“名称”和“分数”,我需要根据B.txt更新tb_name_score的SCORE列。
B.txt也以GBK编码
因此,我遍历B.txt,读取一行并使用它的“名称”值与tb_name_score.NAME中的记录进行比较,如果它们相等,那么我将更新tb_name_score.SCORE。
但是,尽管B.txt中该行的“名称”列与tb_name_score.NAME中的值是相同的中文字符串,但“ =”返回false,我只是无法更新该表。
有人可以帮忙吗?谢谢!

最佳答案

希望能帮助到你:

Python 2.7.3 (default, Apr 10 2013, 06:20:15)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a=u'后者'
>>> b='后者'
>>> type(a)
<type 'unicode'>
>>> type(b)
<type 'str'>
>>> a==b
__main__:1: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
False
>>> b
'\xe5\x90\x8e\xe8\x80\x85'
>>> a
u'\u540e\u8005'
>>> b.decode('utf8')
u'\u540e\u8005'
>>> a.encode('utf8')
'\xe5\x90\x8e\xe8\x80\x85'
>>>

09-27 00:17
查看更多