我正在阅读本书的Python速成课程。我正在使用python3。 在第16章中,有这样的代码:
import csv
filename = '/Users/pc/Desktop/CS
Book/Python_crash_course_practice/16.1/sitka_weather_07-2014.csv'
with open(filename) as f:
reader = csv.reader(f)
header_row = next(reader)
print(header_row)
但是它不起作用,并给我一个如下错误。我不知道该怎么解决。有没有人可以帮助我?谢谢。
%run -i "/Users/pc/Desktop/CS Book/Python_crash_course_practice/16.1/highs_lows.py"
---------------------------------------------------------------------------
UnicodeDecodeError Traceback (most recent call last)
/Users/pc/Desktop/CS Book/Python_crash_course_practice/16.1/highs_lows.py in <module>()
4 with open(filename) as f:
5 reader = csv.reader(f)
----> 6 header_row = next(reader)
7 print(header_row)
8
/Users/pc/Library/Enthought/Canopy/edm/envs/User/lib/python3.5/encodings/ascii.py in decode(self, input, final)
24 class IncrementalDecoder(codecs.IncrementalDecoder):
25 def decode(self, input, final=False):
---> 26 return codecs.ascii_decode(input, self.errors)[0]
27
28 class StreamWriter(Codec,codecs.StreamWriter):
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 1233: ordinal not in range(128)
答案 0 :(得分:0)
文件中包含非ASCII字符,因此您需要在读取时指定文件编码。 您还可以在python脚本的第一行中添加此代码,以将utf-8设置为默认编码
#!/usr/bin/env python # -*- coding: utf-8 -*-