我希望将存储在我的文件夹中的多个.xls
文件转换为.csv
格式。到目前为止,我得到的是:
import glob
import os
import csv
import pandas as pd
path = r'C:\Users\XXX\Desktop\Test'
full_path = os.path.join(path, '*.xls')
for filename in glob.glob(full_path):
name_xls = os.path.basename(filename)
name_csv = name_xls.replace('.xls', '.csv')
data_xls = pd.read_excel(name_xls)
data_xls.to_csv(name_csv, sep=';', encoding='ASCI')
即使我下载了熊猫和xlrd库,我也会遇到以下错误:
Traceback (most recent call last): File
"C:\Users\XXX\.thonny\BundledPython36\lib\site-packages\pandas\io\excel.py",
line 261, in __init__
**import xlrd ModuleNotFoundError: No module named 'xlrd'**
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File
"C:\Users\XXX\Desktop\coverage_code_0\coverage_code_0.py", line
16, in <module>
data_xls = pd.read_excel(name_xls) File
C:\Users\XXX\.thonny\BundledPython36\lib\site-packages\pandas\util\_decorators.py",
line 118, in wrapper
return func(*args, **kwargs) File "C:\Users\XXX\.thonny\BundledPython36\lib\site-packages\pandas\io\excel.py",
line 230, in read_excel
io = ExcelFile(io, engine=engine) File "C:\Users\XXX\.thonny\BundledPython36\lib\site-packages\pandas\io\excel.py",
line 263, in __init__
raise ImportError(err_msg) ImportError: Install xlrd >= 0.9.0 for Excel support
当我将编译器包括在内时,import xlrd
不起作用:
No module named 'xlrd'
我相信我的代码中有一个错误,但我不知道在哪里。有什么想法吗?
发布于 2018-07-27 11:04:07
您需要在同一个解释器和virutalenv中运行pip install xlrd
。在你的评论中,你说你在c:\users\XXX\appdata\local\programs\python\python36-32
上安装了c:\users\XXX\appdata\local\programs\python\python36-32
,但是你的熊猫在C:\Users\XXX\.thonny\BundledPython36\
。如果不使用virtualenv,请尝试在pip
文件夹中查找并运行它。
C:\Users\XXX\.thonny\BundledPython36\...\pip install xlrd
如果使用virtualenv,则只需运行激活它并运行pip install xlrd
。
发布于 2018-07-27 11:06:24
如果您这样做,您需要这个包:
def convert_to_csv():
PATH = path_to_excel
fileNames = os.listdir(PATH)
fileNames = [file for file in fileNames if '.xls' in file]
for file in fileNames:
exl = pd.read_excel(PATH+file)
exl.to_csv(PATH+file[:-3]+'csv',sep=';', index=False, header=True)
if __name__ == "__main__":
import pandas as pd
convert_to_csv()
https://stackoverflow.com/questions/51556281
复制相似问题