2017-04-10 183 views
-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

+0

这是哪个版本的python? – tdelaney

+1

你忘了写BOM吗? –

+1

你写的所有数据都是ascii?如果没有非ascii字符,ascii和utf-8看起来完全相同。 – tdelaney

回答

0

写作.csv格式使用Unicode,例如:

import io, csv 

outfile = 'test/out.csv' 
fieldnames = ['field1', 'field2'] 
content_dict = {'field1':'John', 'field2':'Doo'} 

with io.open(outfile, 'w', newline='', encoding='utf-8') as csv_out: 
    writer = csv.DictWriter(csv_out, fieldnames=fieldnames) 
    writer.writeheader() 

    for row_dict in content_dict: 
     writer.writerow(row_dict)