相关疑难解决方法(0)

Python 2和3 csv阅读器

我正在尝试使用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"循环中的行.那么......我该怎么做?

python csv encoding python-3.x

10
推荐指数
1
解决办法
9060
查看次数

标签 统计

csv ×1

encoding ×1

python ×1

python-3.x ×1