gpt4 book ai didi

python - 使用python将多个excel文件转换为csv文件

转载 作者:太空宇宙 更新时间:2023-11-03 18:02:24 26 4
gpt4 key购买 nike

有没有办法转换多个文件?我可以使用glob.glob吗?

import sys

sys.path.insert(0,'D:/apera/Python27/xlrd-0.9.3')

import xlrd

import csv

ExcelFile = "D:/apera/Workspace/Sounding/sounding010.xls"
CSVFile = "D:/apera/Workspace/Sounding/sounding010.csv"

def Convert(ExcelFile, SheetName, CSVFile):
wb = xlrd.open_workbook(ExcelFile)
ws = wb.sheet_by_name(SheetName)
csvfile = open(CSVFile, 'wb')
wr = csv.writer(csvfile, quoting=csv.QUOTE_ALL, delimiter=';')

for rownum in xrange(ws.nrows):
wr.writerow(
list(x.encode('latin1')
for x in ws.row_values(rownum)))

csvfile.close()

Convert(ExcelFile, "INPUT", CSVFile)

最佳答案

是的,glob 与 os 结合起来就可以了

import os
import glob

os.chdir("yourfolder")
for f in glob.glob("*.xls"):
#call your conversion function

关于python - 使用python将多个excel文件转换为csv文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27471101/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com