【问题标题】:python pandas read_excel engine=openpyxl not closing filepython pandas read_excel engine=openpyxl不关闭文件
【发布时间】:2021-02-18 12:56:31
【问题描述】:

我正在使用以下方法将数据框加载到熊猫中:

import pandas as pd

df_factor_histories=pd.read_excel("./eco_factor/eco_factor_test_data_builder.xlsx",
                                  engine='openpyxl', sheet_name=0)

需要engine=openpyxl 才能启用read_excel 以支持更新的Excel 文件格式(特别是在我的情况下是.xlsx 而不是jusy .xls)。

数据框加载正常但文件保持打开状态

import psutil

p = psutil.Process()
print(p.open_files())

OUTPUT
[popenfile(path='C:\\Users\\xx\\.ipython\\profile_default\\history.sqlite', fd=-1), 
popenfile(path='C:\\Windows\\System32\\en-US\\KernelBase.dll.mui', fd=-1), 
popenfile(path='C:\\Windows\\System32\\en-US\\kernel32.dll.mui', fd=-1), 
popenfile(path='D:\\xxxxx\\data modelling\\eco_factor\\eco_factor_test_data_builder.xlsx', fd=-1)]

Github Post 表明该错误已修复 - 但不适用于我(运行 Anaconda/Jupyter)。 我正在运行的相关版本:

numpy                         1.19.2
openpyxl                      3.0.5
pandas                        1.1.3
Python 3.7.4

我将不胜感激有关如何关闭文件/解决此问题的最佳方法的一些建议,谢谢

【问题讨论】:

    标签: python pandas openpyxl file-handling


    【解决方案1】:

    我建议从您的代码中删除 engine='openpyxl'。它实际上是不需要的。我使用不带它的pd.read_excel,即使.xlsx 格式也能正常工作。

    删除它会导致引擎参数的默认行为接管。引擎将知道使用哪个引擎: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_excel.html#pandas.read_excel

    engine : str, default None
    If io is not a buffer or path, this must be set to identify io. Supported engines: “xlrd”, “openpyxl”, “odf”, “pyxlsb”. Engine compatibility :
    
    “xlrd” supports old-style Excel files (.xls).
    
    “openpyxl” supports newer Excel file formats.
    
    “odf” supports OpenDocument file formats (.odf, .ods, .odt).
    
    “pyxlsb” supports Binary Excel files.
    
    Changed in version 1.2.0: The engine xlrd now only supports old-style .xls files. When engine=None, the following logic will be used to determine the engine:
    
    If path_or_buffer is an OpenDocument format (.odf, .ods, .odt), then odf will be used.
    
    Otherwise if path_or_buffer is an xls format, xlrd will be used.
    
    Otherwise if openpyxl is installed, then openpyxl will be used.
    
    Otherwise if xlrd >= 2.0 is installed, a ValueError will be raised.
    
    Otherwise xlrd will be used and a FutureWarning will be raised. This case will raise a ValueError in a future version of pandas.
    

    【讨论】:

    • 感谢您的建议和补充信息 - 它适用于我当前的应用程序,但几天前我发现自己将它包含在另一个应用程序中,因为没有它.xlsx 将无法加载 - 我感谢你接受时间。
    【解决方案2】:

    我遇到了同样的问题,

    当我使用带有 engine=openpyxl 的 pandas 读取 excel 文件时。它没有被关闭。当我尝试使用 python 归档/移动 excel 文件时,它给出了错误,

    PermissionError: [WinError 32] The process cannot access the file because it is being used by another process
    

    另外,一旦被pandas读取,我们无法使用Excel工具对excel文件进​​行编辑或任何其他操作。

    以下解决方案对我有用。 我正在使用:

    python version 3.6.8.
    

    熊猫==0.25.1

    openpyxl==3.0.7

    import io
    import pandas as pd
    
    with open('path/to/input_excel_file.xlsx', "rb") as f:
        file_io_obj = io.BytesIO(f.read())
    
    
    df_input_file = pd.read_excel(file_io_obj, engine='openpyxl', sheet_name=None)
    

    【讨论】:

      猜你喜欢
      • 2018-05-21
      • 2019-01-01
      • 2021-06-19
      • 1970-01-01
      • 2015-08-26
      • 1970-01-01
      • 2020-02-08
      • 2022-07-11
      • 1970-01-01
      相关资源
      最近更新 更多