使用 Python 2.7 读取和写入包括 unicode 的 CSV 文件
- 2025-02-21 08:50:00
- admin 原创
- 76
问题描述:
我是 Python 新手,对如何使用 Python 读写 CSV 文件有疑问。我的文件包含德国、法国等。根据我的代码,这些文件可以在 Python 中正确读取,但是当我将其写入新的 CSV 文件时,unicode 变成了一些奇怪的字符。
数据如下:
我的代码是:
import csv
f=open('xxx.csv','rb')
reader=csv.reader(f)
wt=open('lll.csv','wb')
writer=csv.writer(wt,quoting=csv.QUOTE_ALL)
wt.close()
f.close()
结果如下:
我该怎么做才能解决这个问题?
解决方案 1:
另一种选择:
使用来自 unicodecsv 包的代码...
https://pypi.python.org/pypi/unicodecsv/
>>> import unicodecsv as csv
>>> from io import BytesIO
>>> f = BytesIO()
>>> w = csv.writer(f, encoding='utf-8')
>>> _ = w.writerow((u'é', u'ñ'))
>>> _ = f.seek(0)
>>> r = csv.reader(f, encoding='utf-8')
>>> next(r) == [u'é', u'ñ']
True
该模块的 API 与 STDLIB csv 模块兼容。
解决方案 2:
确保您进行适当的编码和解码。
此示例将把一些 utf-8 格式的示例文本转储到 csv 文件并返回以进行演示:
# -*- coding: utf-8 -*-
import csv
tests={'German': [u'Straße',u'auslösen',u'zerstören'],
'French': [u'français',u'américaine',u'épais'],
'Chinese': [u'中國的',u'英語',u'美國人']}
with open('/tmp/utf.csv','w') as fout:
writer=csv.writer(fout)
writer.writerows([tests.keys()])
for row in zip(*tests.values()):
row=[s.encode('utf-8') for s in row]
writer.writerows([row])
with open('/tmp/utf.csv','r') as fin:
reader=csv.reader(fin)
for row in reader:
temp=list(row)
fmt=u'{:<15}'*len(temp)
print fmt.format(*[s.decode('utf-8') for s in temp])
印刷:
German Chinese French
Straße 中國的 français
auslösen 英語 américaine
zerstören 美國人 épais
解决方案 3:
csv 模块文档末尾有一个示例,演示了如何处理 Unicode。下面直接从该示例复制。请注意,读取或写入的字符串将是 Unicode 字符串。UnicodeWriter.writerows
例如,不要将字节字符串传递给。
import csv,codecs,cStringIO
class UTF8Recoder:
def __init__(self, f, encoding):
self.reader = codecs.getreader(encoding)(f)
def __iter__(self):
return self
def next(self):
return self.reader.next().encode("utf-8")
class UnicodeReader:
def __init__(self, f, dialect=csv.excel, encoding="utf-8-sig", **kwds):
f = UTF8Recoder(f, encoding)
self.reader = csv.reader(f, dialect=dialect, **kwds)
def next(self):
'''next() -> unicode
This function reads and returns the next line as a Unicode string.
'''
row = self.reader.next()
return [unicode(s, "utf-8") for s in row]
def __iter__(self):
return self
class UnicodeWriter:
def __init__(self, f, dialect=csv.excel, encoding="utf-8-sig", **kwds):
self.queue = cStringIO.StringIO()
self.writer = csv.writer(self.queue, dialect=dialect, **kwds)
self.stream = f
self.encoder = codecs.getincrementalencoder(encoding)()
def writerow(self, row):
'''writerow(unicode) -> None
This function takes a Unicode string and encodes it to the output.
'''
self.writer.writerow([s.encode("utf-8") for s in row])
data = self.queue.getvalue()
data = data.decode("utf-8")
data = self.encoder.encode(data)
self.stream.write(data)
self.queue.truncate(0)
def writerows(self, rows):
for row in rows:
self.writerow(row)
with open('xxx.csv','rb') as fin, open('lll.csv','wb') as fout:
reader = UnicodeReader(fin)
writer = UnicodeWriter(fout,quoting=csv.QUOTE_ALL)
for line in reader:
writer.writerow(line)
输入(UTF-8 编码):
American,美国人
French,法国人
German,德国人
输出:
"American","美国人"
"French","法国人"
"German","德国人"
解决方案 4:
因为str
在python2中bytes
实际上是。所以如果要写入unicode
csv,必须使用编码unicode
进行编码。str
`utf-8`
def py2_unicode_to_str(u):
# unicode is only exist in python2
assert isinstance(u, unicode)
return u.encode('utf-8')
使用class csv.DictWriter(csvfile, fieldnames, restval='', extrasaction='raise', dialect='excel', *args, **kwds)
:
py2
+ 这`csvfile`:`open(fp, 'w')`
+ 传递密钥和值,其中`bytes`编码为`utf-8`
- `writer.writerow({py2_unicode_to_str(k): py2_unicode_to_str(v) for k,v in row.items()})`
py3
+ 这`csvfile`:`open(fp, 'w')`
+ 传递包含以下内容的普通`str`字典`row`:`writer.writerow(row)`
最后代码
import sys
is_py2 = sys.version_info[0] == 2
def py2_unicode_to_str(u):
# unicode is only exist in python2
assert isinstance(u, unicode)
return u.encode('utf-8')
with open('file.csv', 'w') as f:
if is_py2:
data = {u'Python中国': u'Python中国', u'Python中国2': u'Python中国2'}
# just one more line to handle this
data = {py2_unicode_to_str(k): py2_unicode_to_str(v) for k, v in data.items()}
fields = list(data[0])
writer = csv.DictWriter(f, fieldnames=fields)
for row in data:
writer.writerow(row)
else:
data = {'Python中国': 'Python中国', 'Python中国2': 'Python中国2'}
fields = list(data[0])
writer = csv.DictWriter(f, fieldnames=fields)
for row in data:
writer.writerow(row)
结论
在python3中,只需使用unicode str
。
在python2中,使用unicode
handle文本,str
在发生I/O时使用。
解决方案 5:
我遇到了同样的问题。答案是你已经做对了。这是 MS Excel 的问题。尝试使用其他编辑器打开文件,你会注意到你的编码已经成功了。为了让 MS Excel 满意,请从 UTF-8 移动到 UTF-16。这应该有效:
class UnicodeWriter:
def __init__(self, f, dialect=csv.excel_tab, encoding="utf-16", **kwds):
# Redirect output to a queue
self.queue = StringIO.StringIO()
self.writer = csv.writer(self.queue, dialect=dialect, **kwds)
self.stream = f
# Force BOM
if encoding=="utf-16":
import codecs
f.write(codecs.BOM_UTF16)
self.encoding = encoding
def writerow(self, row):
# Modified from original: now using unicode(s) to deal with e.g. ints
self.writer.writerow([unicode(s).encode("utf-8") for s in row])
# Fetch UTF-8 output from the queue ...
data = self.queue.getvalue()
data = data.decode("utf-8")
# ... and reencode it into the target encoding
data = data.encode(self.encoding)
# strip BOM
if self.encoding == "utf-16":
data = data[2:]
# write to the target stream
self.stream.write(data)
# empty queue
self.queue.truncate(0)
def writerows(self, rows):
for row in rows:
self.writerow(row)
解决方案 6:
我无法回应上面的 Mark,但我刚刚做了一个修改,修复了单元格中的数据不是 unicode(例如浮点或整数数据)时导致的错误。我将这一行替换为 UnicodeWriter 函数:“self.writer.writerow([s.encode("utf-8") if type(s)==types.UnicodeType else s for s in row])”,这样它就变成了:
class UnicodeWriter:
def __init__(self, f, dialect=csv.excel, encoding="utf-8-sig", **kwds):
self.queue = cStringIO.StringIO()
self.writer = csv.writer(self.queue, dialect=dialect, **kwds)
self.stream = f
self.encoder = codecs.getincrementalencoder(encoding)()
def writerow(self, row):
'''writerow(unicode) -> None
This function takes a Unicode string and encodes it to the output.
'''
self.writer.writerow([s.encode("utf-8") if type(s)==types.UnicodeType else s for s in row])
data = self.queue.getvalue()
data = data.decode("utf-8")
data = self.encoder.encode(data)
self.stream.write(data)
self.queue.truncate(0)
def writerows(self, rows):
for row in rows:
self.writerow(row)
您还需要“导入类型”。
解决方案 7:
我认为这不是最好的答案,但它可能是最独立的答案,也是最有趣的答案。
UTF7是 unicode 的 7 位 ASCII 编码。碰巧的是,UTF7 不会特别使用逗号、引号或空格。它只是将它们从输入传递到输出。因此,如果您先进行 UTF7 编码然后解析为 CSV,或者先解析为 CSV 然后进行 UTF7 编码,实际上没有什么区别。Python 2 的 CSV 解析器无法处理 unicode,但 Python 2 确实有一个 UTF-7 编码器。因此,您可以编码、解析,然后解码,就像您有一个支持 unicode 的解析器一样。
import csv
import io
def read_csv(path):
with io.open(path, 'rt', encoding='utf8') as f:
lines = f.read().split("
")
lines = [l.encode('utf7').decode('ascii') for l in lines]
reader = csv.reader(lines, dialect=csv.excel)
for row in reader:
yield [x.encode('ascii').decode('utf7') for x in row]
for row in read_csv("lol.csv"):
print(repr(row))
哈哈.csv
foo,bar,foo∆bar,"foo,bar"
输出:
[u'foo', u'bar', u'foo/u2206bar', u'foo,bar']
扫码咨询,免费领取项目管理大礼包!