简体   繁体   中英

Python : open csv files from a list and convert them into xls

I'd like to open multiple csv files from a list and then convert them into xls files.

I made that code :

import sys, csv, xlwt

files = ['/home/julien/excel/csv/ABORD2.csv']

for i in files:
    f=open(i, 'rb')
    g = csv.reader ((f), delimiter=";")
    workbook=xlwt.Workbook()
    sheet= xlwt.Workbook()
    sheet = workbook.add_sheet("Sheet 1")

    for rowi, row in enumerate(g):
        for coli, value in enumerate(row):
            sheet.write(rowi,coli,value)
        workbook.save(i + ".xls")

My xls files are created.But in both of them I only have the path of the xls. For example for the file ABORD.xls only the following expression is written :

'/home/julien/excel/csv/ABORD2.xls'

Would you have any suggestions ?

Sir, you're creating two Workbooks unnecessairly and you're saving the workbook with wrong identation

import csv, xlwt

files = ['test.csv']

for i in files:
    f=open(i, 'rb')
    g = csv.reader ((f), delimiter=";")
    wbk= xlwt.Workbook()
    sheet = wbk.add_sheet("Sheet 1")

    for rowi, row in enumerate(g):
        for coli, value in enumerate(row):
            sheet.write(rowi,coli,value)

    wbk.save(i + '.xls')

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