我正在尝试使用csv模块读取utf-8 csv文件,由于编码,我在创建python 2和3的通用代码时遇到了一些麻烦.
这是Python 2.7中的原始代码:
with open(filename, 'rb') as csvfile:
csv_reader = csv.reader(csvfile, quotechar='\"')
langs = next(csv_reader)[1:]
for row in csv_reader:
pass
Run Code Online (Sandbox Code Playgroud)
但是当我用python 3运行它时,它不喜欢我没有"编码"打开文件的事实.我试过这个:
with codecs.open(filename, 'r', encoding='utf-8') as csvfile:
csv_reader = csv.reader(csvfile, quotechar='\"')
langs = next(csv_reader)[1:]
for row in csv_reader:
pass
Run Code Online (Sandbox Code Playgroud)
现在python 2无法解码"for"循环中的行.那么......我该怎么做?