UnicodeEncodeError: 'ascii' codec can't encode characters in position 23-24: ordinal not in range(128)
本问题已经有最佳答案,请猛点这里访问。
我正在尝试分析包含以下文本的csv(test.csv)
1 2 | "legalgroup_text" "Aktiebolag" "Aktiebolag" "LGAKTIEBOLAG" "legalgroup_text" "Allm?n f?rs?kringskassa" "Allm?n f?rs?kringskassa" "LGALLMAENFOERSAEKRINGSKASSA" |
我正在使用编码
1 2 3 4 5 6 7 | 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) |
我出错了
1 2 3 4 | 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) |
我也试过编码
1 | UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in position 25: ordinal not in range(128) |
放弃python 2,使用python 3。这本身(没有其他的变化,我刚刚测试)将解决这个问题。