熊猫read_excel()导入xlrd失败

时间:2018-07-27 10:42:23

标签: python excel pandas csv xlrd

我想将文件夹中存储的多个.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')

即使我下载了 pandas和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'

我相信我的代码中有错误,但是我不知道在哪里。有什么想法吗?

2 个答案:

答案 0 :(得分:2)

您需要在安装了熊猫的同一个解释器和virutalenv中运行pip install xlrd。在注释中,您说xlrd中已安装c:\users\XXX\appdata\local\programs\python\python36-32,但熊猫在{ {1}}。如果您不使用virtualenv,请尝试在C:\Users\XXX\.thonny\BundledPython36\文件夹中找到pip并运行它。

BundledPython36

如果您使用virtualenv,则activate it并只需运行C:\Users\XXX\.thonny\BundledPython36\...\pip install xlrd

答案 1 :(得分:0)

如果这样做,则需要此软件包:

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()