我正在尝试使用以下代码导入 CSV:
import csv
import sys
def load_csv(filename):
# Open file for reading
file = open(filename, 'r')
# Read in file
return csv.reader(file, delimiter=',', quotechar='\n')
def main(argv):
csv_file = load_csv("myfile.csv")
for item in csv_file:
print(item)
if __name__ == "__main__":
main(sys.argv[1:])
这是我的 csv 文件的示例:
foo,bar,test,1,2
this,wont,work,because,α
和错误:
Traceback (most recent call last):
File "test.py", line 22, in <module>
main(sys.argv[1:])
File "test.py", line 18, in main
for item in csv_file:
File "/usr/lib/python3.2/encodings/ascii.py", line 26, in decode
return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xce in position 40: ordinal not in range(128)
显然,它击中了 CSV 末尾的字符并引发了该错误,但我不知道如何解决这个问题。有什么帮助吗?
这是:
Python 3.2.3 (default, Apr 23 2012, 23:35:30)
[GCC 4.7.0 20120414 (prerelease)] on linux2