在 pandas 中解码为 UTF-8
Decode to UTF-8 in pandas
我有包含此内容的 csv 文件 - 如您所见,某些字段行不是字符串值。我使用此命令读取文件:
data = gpd.read_file('data.csv', encoding='utf8')
CSV 文件:
笔记本:
如您所见,column name
仍未解码。我已经尝试了以下命令,但没有成功,因为它将列视为 str
,并且无法在其上调用 decode()
函数。
data['name'] = data['name'].apply(lambda x:x.decode('utf8', 'strict') if not isinstance(x, str) else x)
有效:
data['name'] = data['name'].apply(
lambda x:x[2:-1].encode().decode("unicode_escape").encode('raw_unicode_escape').decode()
)
循序渐进
在:
x = r"b'\xd9\x85\xd9\x86\xd8\xaa\xd8\xb2\xd9\x87\xd8\xb1\xd8\xa7\xd8\xa8'"
print(f"x {type(x)}\n\t= {x}\n")
x = x[2:-1]
print(f"x[2:-1] {type(x)}\n\t= {x}\n")
x = x.encode()
print(f"x[2:-1].encode() {type(x)}\n\t= {x}\n")
x = x.decode("unicode_escape").encode('raw_unicode_escape')
print(f"x[2:-1].encode().decode('unicode_escape').encode('raw_unicode_escape') {type(x)}\n\t= {x}\n")
x = x.decode()
print(f"x[2:-1].encode().decode('unicode_escape').encode('raw_unicode_escape').decode() {type(x)}\n\t= {x}\n")
输出:
x <class 'str'>
= b'\xd9\x85\xd9\x86\xd8\xaa\xd8\xb2\xd9\x87\xd8\xb1\xd8\xa7\xd8\xa8'
x[2:-1] <class 'str'>
= \xd9\x85\xd9\x86\xd8\xaa\xd8\xb2\xd9\x87\xd8\xb1\xd8\xa7\xd8\xa8
x[2:-1].encode() <class 'bytes'>
= b'\xd9\x85\xd9\x86\xd8\xaa\xd8\xb2\xd9\x87\xd8\xb1\xd8\xa7\xd8\xa8'
x[2:-1].encode().decode('unicode_escape').encode('raw_unicode_escape') <class 'bytes'>
= b'\xd9\x85\xd9\x86\xd8\xaa\xd8\xb2\xd9\x87\xd8\xb1\xd8\xa7\xd8\xa8'
x[2:-1].encode().decode('unicode_escape').encode('raw_unicode_escape').decode() <class 'str'>
= منتزهراب
我有包含此内容的 csv 文件 - 如您所见,某些字段行不是字符串值。我使用此命令读取文件:
data = gpd.read_file('data.csv', encoding='utf8')
CSV 文件:
笔记本:
如您所见,column name
仍未解码。我已经尝试了以下命令,但没有成功,因为它将列视为 str
,并且无法在其上调用 decode()
函数。
data['name'] = data['name'].apply(lambda x:x.decode('utf8', 'strict') if not isinstance(x, str) else x)
有效:
data['name'] = data['name'].apply(
lambda x:x[2:-1].encode().decode("unicode_escape").encode('raw_unicode_escape').decode()
)
循序渐进
在:
x = r"b'\xd9\x85\xd9\x86\xd8\xaa\xd8\xb2\xd9\x87\xd8\xb1\xd8\xa7\xd8\xa8'"
print(f"x {type(x)}\n\t= {x}\n")
x = x[2:-1]
print(f"x[2:-1] {type(x)}\n\t= {x}\n")
x = x.encode()
print(f"x[2:-1].encode() {type(x)}\n\t= {x}\n")
x = x.decode("unicode_escape").encode('raw_unicode_escape')
print(f"x[2:-1].encode().decode('unicode_escape').encode('raw_unicode_escape') {type(x)}\n\t= {x}\n")
x = x.decode()
print(f"x[2:-1].encode().decode('unicode_escape').encode('raw_unicode_escape').decode() {type(x)}\n\t= {x}\n")
输出:
x <class 'str'>
= b'\xd9\x85\xd9\x86\xd8\xaa\xd8\xb2\xd9\x87\xd8\xb1\xd8\xa7\xd8\xa8'
x[2:-1] <class 'str'>
= \xd9\x85\xd9\x86\xd8\xaa\xd8\xb2\xd9\x87\xd8\xb1\xd8\xa7\xd8\xa8
x[2:-1].encode() <class 'bytes'>
= b'\xd9\x85\xd9\x86\xd8\xaa\xd8\xb2\xd9\x87\xd8\xb1\xd8\xa7\xd8\xa8'
x[2:-1].encode().decode('unicode_escape').encode('raw_unicode_escape') <class 'bytes'>
= b'\xd9\x85\xd9\x86\xd8\xaa\xd8\xb2\xd9\x87\xd8\xb1\xd8\xa7\xd8\xa8'
x[2:-1].encode().decode('unicode_escape').encode('raw_unicode_escape').decode() <class 'str'>
= منتزهراب