我有以下代码来读取 csv 中的列:
# ------------------------------------------------------------------------------
# READS CSV, RETURNS LIST OF COLUMN DATA, FROM ROW rowStart TO rowFinish
# ------------------------------------------------------------------------------
def csvColumnAsList(csvFileName, column, rowStart, rowFinish):
column_list = [] # defines it as a list
with codecs.open(csvFileName, 'r', 'utf-8') as csvfile:
spamreader = csv.reader(csvfile, delimiter=',')
for row in itertools.islice(spamreader, rowStart, rowFinish+1):
column_list.append(row[column].decode('utf8'))
return column_list
但这会触发错误:
File "algo-8.py", line 51, in csvColumnAsList
for row in itertools.islice(spamreader, rowStart, rowFinish+1): UnicodeEncodeError: 'ascii' codec can't encode character u'\u201c' in
position 215: ordinal not in range(128)
有人能帮我吗。我尝试了一些编码,解码的方法,但还没有弄清楚。