对xlwt进行了一个简单的包装,便于按行输出。
以后给国内客户尽量提供xls格式的输出(utf-8格式的csv不方便Excel用户使用)。
鉴于32位系统下Excel的65535行数的限制,XLSWriter进行了自动分表。
# coding: utf-8 # xlswriter.py # http://pypi.python.org/pypi/xlwt import xlwt class XLSWriter(object): """A XLS writer that produces XLS files from unicode data. """ def __init__(self, file, encoding='utf-8'): # must specify the encoding of the input data, utf-8 default. self.file = file self.encoding = encoding self.wbk = xlwt.Workbook() self.sheet_index = 0 self.header = [] self.create_sheet() def create_sheet(self): self.sheet_index += 1 self.sheet = self.wbk.add_sheet('sheet %d' % self.sheet_index, cell_overwrite_ok=True) self.rows_num = 0 def cell(self, s): if isinstance(s, basestring): if not isinstance(s, unicode): s = s.decode(self.encoding) elif s is None: s = '' else: s = str(s) return s def writerow(self, row): if self.rows_num == 0: self.header = row if self.rows_num >= 65534: self.save() self.create_sheet() if self.header: self.writerow(self.header) for ci, col in enumerate(row): self.sheet.write(self.rows_num, ci , self.cell(col)) self.rows_num += 1 def writerows(self, rows): for row in rows: self.writerow(row) def save(self): self.wbk.save(self.file) if __name__ == '__main__': import csv # test # convert a existing CSV file to XLS file xlswriter = XLSWriter('hc360.xls') for row in csv.reader(open('hc360.csv')): xlswriter.writerow(row) # don't forget to save data to disk xlswriter.save() print 'finished.'
呵呵,谢谢
VaTG790i.最好的<a href=http://www.kyfei.com>网站推广软件</a>,
非常好
....................
;ui;普i;uighur;ui;ui;个
在unix网络编程中看到了关于TCP/IP的一些内容,我感觉还是写的不够。正在下载中,一定
下载地址呢