如何在python程序中比较非英文(中文)字符?
在我的一个python程序(python 2.7)中,我需要处理一些汉字:如何在python程序中比较非英文(中文)字符?,python,mysql,gbk,Python,Mysql,Gbk,在我的一个python程序(python 2.7)中,我需要处理一些汉字: 我有一个文件a.txt,它有两列:“name”和“score”,“name”列可以对一些中文字符串进行赋值,score是一个介于1和10之间的整数。A.txt是用GBK编码的,GBK是一种汉字编码 我将A.txt的每一行插入mysql表tb_name_score,它有三列:ID、name、score,其name列的编码为latin1\u swedish\u ci 现在,我有另一个文件名B.txt,它也有两列,“name”
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'
>>>
Python 2.7.3(默认,2013年4月10日06:20:15)
[GCC 4.6.3]关于linux2
有关详细信息,请键入“帮助”、“版权”、“信用证”或“许可证”。
>>>a=u'后者'
>>>b='后者'
>>>类型(a)
>>>类型(b)
>>>a==b
__main_uuuu;:1:UnicodeWarning:Unicode相等比较无法将两个参数转换为Unicode-将它们解释为不相等
假的
>>>b
“\xe5\x90\x8e\xe8\x80\x85”
>>>a
u'\u540e\u8005'
>>>b.解码('utf8')
u'\u540e\u8005'
>>>a.encode('utf8')
“\xe5\x90\x8e\xe8\x80\x85”
>>>
df_raw=pd.read_excel('/Users/zh/workspace/CityRealEstate/CityDataset20180521-4.xlsx'))
df_train=df_raw.iloc[:,3:59]
打印df_raw.loc[df_raw['Year']2016]
城市深圳'
打印df_原始[“城市]]值
df_train=df_raw.loc[df_raw['City']==City.decode('utf8')]
它适合我Python 2还是Python 3?还有,为什么你在使用中文字符时使用不区分大小写的拉丁文1和瑞典语言环境?我是新手,所以我和团队中的其他人一样。
df_raw=pd.read_excel('/Users/zh/workspace/CityRealEstate/CityDataset20180521-4.xlsx')
df_train = df_raw.iloc[:,3:59]
print df_raw.loc[df_raw['Year'] <> 2016]
city = '深圳'
print df_raw['City'].values
df_train=df_raw.loc[df_raw['City'] == city.decode('utf8')]