问题描述
我使用的是 Python 2.7.12.使用此代码片段,我正在保存一个 utf-8 csv 文件.我在文件开头写了 BOM(字节顺序标记).
I'm using Python 2.7.12. With this code snippet I'm saving a utf-8 csv file. I wrote the BOM (byte order mark) at the beginning of the file.
import codecs
import csv
outputFile = open("test.csv", "wb")
outputFile.write(codecs.BOM_UTF8)
fieldnames = ["a", "b"]
writer = csv.DictWriter(outputFile, fieldnames, delimiter=";")
writer.writeheader()
row = dict([])
for i in range(10):
row["a"] = str(i).encode("utf-8")
row["b"] = str(i*2).encode("utf-8")
writer.writerow(row)
outputFile.close()
我想加载那个 csv 文件:
I want to load that csv file:
import codecs
import csv
inputFile = open("test.csv", "rb")
reader = csv.DictReader(inputFile, delimiter=";")
for row in reader:
print row["a"]
inputFile.close()
上面的代码会失败:KeyError: 'a'
如果我打印行键,它们的外观是这样的:[u'ufeffa', u'b']
.BOM 已嵌入到键 a
中.我做错了什么?
The above code is going to fail: KeyError: 'a'
If I print the row keys this is how they look: [u'ufeffa', u'b']
. The BOM has been embedded into the key a
. What am I doing wrong?
推荐答案
你必须告诉 open 这是带有 BOM 的 UTF-8.我知道这适用于 io.open:
You have to tell open that this is UTF-8 with BOM. I know that works with io.open:
import io
.
.
.
inputFile = io.open("test.csv", "r", encoding='utf-8-sig')
.
.
.
你必须以文本模式打开文件,r"而不是rb".
And you have to open the file in text mode, "r" instead of "rb".
这篇关于Python读取csv-嵌入到第一个键中的BOM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!