简体   繁体   中英

Convert a CSV containing text file to excel using python

I am creating a script that will convert a text file to excel. This contains data like this:

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.

I have managed to write a single line to excel, but I am clueless how to write a whole text file.

This is what I have done so far:

#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')

Any other approach is also welcomed.

You could consider using the well known pandas library. It has routines to load from CSV and save to excel .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM