UnicodeEncodeError:' ascii'编解码器无法对位置23-24中的字符进行编码:序数不在范围内(128)

时间:2018-04-19 05:41:08

标签: python python-2.7 python-unicode

我正在尝试解析包含text(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)

1 个答案:

答案 0 :(得分:0)

放弃Python 2并使用Python 3.它本身(没有其他更改,我刚刚测试过)将解决问题。