使用python将包含文本文件的CSV转换为excel

时间:2013-12-24 09:06:34

标签: python excel

我正在创建一个将文本文件转换为excel的脚本。这包含如下数据:

1,20131223100043,0,G1,0,0,918814873380,1,1,0,....73 items ;
1,20131223100042,0,G1,0,0,918814873381,1,1,0,....73 items ;
1,20131223100073,0,G1,0,0,918814873382,1,1,0,....73 items ;
....
...
atleast 1000 lines.

我已经设法写了一行excel,但我无法写一个完整的文本文件。

这是我到目前为止所做的:

#coding: utf8
import xlwt
wbk = xlwt.Workbook()
sheet = wbk.add_sheet('sheet 1')

#Create a style for font and its size for first line
style = xlwt.XFStyle()
font = xlwt.Font()
font.name = 'Arial'
#font.bold = True
font.height = 220
# Set the style's font to this new one you set up
style.font = font

#split lines
string1 = '1,20131223100043,0,G1,0,0,918814873380,1,1,0....all 73 items'
list1 = string1.decode('utf-8').split(',')

for i in range(1, 73):
sheet.write(1,i-1,list1[i-1], style)
wbk.save('text11.xls')

也欢迎任何其他方法。

1 个答案:

答案 0 :(得分:0)

您可以考虑使用众所周知的pandas库。它具有从CSV和save to excel加载的例程。