python中 编码转换unicode

实现代码如下:
a = 'abce'
# print type(a)
b = a.decode("ascii")
# print type(b)
c = a.decode("ascii").encode("utf-8")
# print type(c)


在python中进行编码转换都是通过unicode作为中间值实现的。所以要先decode成unicode字符,然后再使用encode转换成utf-8编码的str。可以把注释取消了,看下转换过程中的类型。

python ascii转unicode 转换不成功
print(chardet.detect(str(row['big_hy_name'])))
        print(chardet.detect(str(row['big_hy_name']).decode("utf-8",'ignore')))

优化前
for k,v in dict1.items():
    for index, row in df1.iterrows():
        if k == row['big_hy_name']:

优化后:
for idx,row in df1.iterrows():
    if row["big_hy_name"] in map_dict.keys():

猜你喜欢

转载自blog.csdn.net/sinat_26566137/article/details/81103255