Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python程序中比较非英文(中文)字符?_Python_Mysql_Gbk - Fatal编程技术网

如何在python程序中比较非英文(中文)字符?

如何在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程序(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”和“score”,我需要根据B.txt更新tb_name_score的score列。 B.txt也用GBK编码

  • 因此,我遍历B.txt,读取一行,并使用它的“name”值与tb_name_score.name中的记录进行比较,如果它们相等,则更新tb_name_score.score。 然而,尽管B.txt中行的“name”列与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'
    >>> 
    
    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')]