将多个.xls文件转换为.csv python
我需要将大量文件从.xls转换为.csv文件。 我尝试使用loop使其:将多个.xls文件转换为.csv python,python,csv,type-conversion,xls,Python,Csv,Type Conversion,Xls,我需要将大量文件从.xls转换为.csv文件。 我尝试使用loop使其: excel_files = glob.glob("A/2018/*xls") for excel in excel_files: out = excel.split('.')+'.csv' df = pd.read_excel(excel) df.to_csv(out) 我得到了一个错误: TypeError T
excel_files = glob.glob("A/2018/*xls")
for excel in excel_files:
out = excel.split('.')+'.csv'
df = pd.read_excel(excel)
df.to_csv(out)
我得到了一个错误:
TypeError Traceback (most recent call last)
<ipython-input-62-a04c590b0fd7> in <module>
1 for excel in excel_files:
----> 2 out = excel.split('.')+'.csv'
3 df = pd.read_excel(excel)
4 df.to_csv(out)
TypeError: can only concatenate list (not "str") to list
TypeError回溯(最近一次调用)
在里面
1对于excel\u文件中的excel:
---->2 out=excel.split('.')+'.csv'
3 df=pd.read\u excel(excel)
4 df.至_csv(输出)
TypeError:只能将列表(而不是“str”)连接到列表
哪些变量以及如何更改?用于此。特别是,该方法提供了一种更改文件扩展名的简单方法
from pathlib import Path
import pandas as pd
excel_files = Path("A/2018").glob("*xls")
for excel_file in excel_files:
df = pd.read_excel(excel_file)
out_path = excel_file.with_suffix(".csv")
df.to_csv(out_path)
.split
返回一个列表,如果你想获取文件名,你应该使用excel.split('.')[0]+'.csv'
。嘿,谢谢你的回复,但是这个方法对我也不起作用。这次我有错误:TypeError:无法从“PosixPath”解析此错误在哪里?您可能必须使用pd.read\u excel(str(excel\u文件))
和df.to\u csv(str(out\u path))