UnicodeEncodeError: 'ascii' codec can't encode characters in position 23-24: ordinal not in range(128)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 23-24: ordinal not in range(128)
我正在尝试解析包含文本下方文本 (test.csv)
的 csv
"legalgroup_text" "Aktiebolag" "Aktiebolag" "LGAKTIEBOLAG"
"legalgroup_text" "Allmän försäkringskassa" "Allmän försäkringskassa" "LGALLMAENFOERSAEKRINGSKASSA"
我正在使用编码 iso-8859-1
,因为文件包含瑞典语字符
import codecs
import csv
with codecs.open('test.csv', encoding='iso-8859-1') as label_file:
data = csv.reader(label_file, delimiter='\t')
for row in data:
print(row)
我遇到错误
Traceback (most recent call last):
File "/mnt/ashraful/PycharmProjects/Test/test.py", line 6, in <module>
for row in data:
UnicodeEncodeError: 'ascii' codec can't encode characters in position 23-24: ordinal not in range(128)
我也尝试过编码 utf-8
,但出现错误
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in position 25: ordinal not in range(128)
放弃 Python 2 并使用 Python 3。它本身(没有其他更改,我刚刚测试过)将解决问题。
我正在尝试解析包含文本下方文本 (test.csv)
的 csv"legalgroup_text" "Aktiebolag" "Aktiebolag" "LGAKTIEBOLAG"
"legalgroup_text" "Allmän försäkringskassa" "Allmän försäkringskassa" "LGALLMAENFOERSAEKRINGSKASSA"
我正在使用编码 iso-8859-1
,因为文件包含瑞典语字符
import codecs
import csv
with codecs.open('test.csv', encoding='iso-8859-1') as label_file:
data = csv.reader(label_file, delimiter='\t')
for row in data:
print(row)
我遇到错误
Traceback (most recent call last):
File "/mnt/ashraful/PycharmProjects/Test/test.py", line 6, in <module>
for row in data:
UnicodeEncodeError: 'ascii' codec can't encode characters in position 23-24: ordinal not in range(128)
我也尝试过编码 utf-8
,但出现错误
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in position 25: ordinal not in range(128)
放弃 Python 2 并使用 Python 3。它本身(没有其他更改,我刚刚测试过)将解决问题。