-1
我希望有人能够帮助我处理部分代码,输出文件有一个问题,应该使用unicode以.csv格式出现,易于阅读在excel上。问题在于输出文件不带格式,而其中的文本以ASCII(7位)形式出现。将Ascii 7位解码为可读的UTF8 .CSV文件
我真的apreaciate你的帮助,我一直在这4小时,现在还不能发现问题:/
脚本的最后一部分:
class UnicodeWriter:
"""
A CSV writer which will write rows to CSV file "f",
which is encoded in the given encoding.
"""
def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
# Redirect output to a queue
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):
self.writer.writerow([s.encode("utf-8").replace("\n"," ").replace("\r"," ").replace("\t",'') 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 = self.encoder.encode(data)
# 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)
Python版本是2.7在Windows 10 是ASCII
这是哪个版本的python? – tdelaney
你忘了写BOM吗? –
你写的所有数据都是ascii?如果没有非ascii字符,ascii和utf-8看起来完全相同。 – tdelaney