简体   繁体   English

Python:从列表中打开csv文件并将它们转换为xls

[英]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. 我想从列表中打开多个csv文件,然后将它们转换为xls文件。

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. 我创建了我的xls文件。但是在这两个文件中我只有xls的路径。 For example for the file ABORD.xls only the following expression is written : 例如,对于文件ABORD.xls,只写入以下表达式:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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