gpt4 book ai didi

python - 为什么 Pandas read_excel 函数在 Pyinstaller .exe 中返回错误,但在 Python 解释器下却没有?

转载 作者:太空宇宙 更新时间:2023-11-04 04:33:50 26 4
gpt4 key购买 nike

我正在使用 Pandas read_excel 函数从电子表格导入数据。这在 Python 解释器下运行时工作正常,但当我使用 PyInstaller 构建 exe 时,它​​返回一个 IndexError。

这是演示问题的简化代码 pandas_test.py:

import pandas as pd

filepath = 'C:/Users/User/Documents/Development/Python/PHL/Test Data/Study
template mock-up.xlsx'
df = pd.read_excel(filepath, sheet_name='Data Entry', index_col=9)
print(df.head())

这在带有 pandas 0.23.4 和 xlrd 1.1.0 的 Python 3.6 下运行得很好。

当我使用 PyInstaller 构建 pandas_test.py 时,它成功生成了一个 pandas_test.exe,但我收到此错误:

Traceback (most recent call last):
File "pandas_test.py", line 4, in <module>
File "site-packages\pandas\io\excel.py", line 212, in read_excel
File "site-packages\pandas\io\excel.py", line 513, in _parse_excel
File "site-packages\pandas\io\parsers.py", line 1912, in TextParser
File "site-packages\pandas\io\parsers.py", line 764, in __init__
File "site-packages\pandas\io\parsers.py", line 995, in _make_engine
File "site-packages\pandas\io\parsers.py", line 2021, in __init__
File "site-packages\pandas\io\parsers.py", line 2772, in _get_index_name
File "site-packages\pandas\io\parsers.py", line 3084, in _clean_index_names
IndexError: list index out of range
[17264] Failed to execute script pandas_test

我已经通读了 PyInstaller 的输出,但没有明显相关的内容:

887 INFO: PyInstaller: 3.3.1
887 INFO: Python: 3.6.2
889 INFO: Platform: Windows-10-10.0.17134-SP0
892 INFO: wrote C:\Users\User\Documents\Development\Python\PandaTest\pandas_test.spec

如果找不到某个模块我能理解,但为什么只在部署的代码中出现 IndexError?

最佳答案

我遇到了与您类似的错误,但在我的情况下没有警告,直到我运行它以获取以下脚本的“检测到 fatal error ”(Excelfile.parse 等同于 read_excel(ExcelFile, ...),只是尝试看看 Pyinstaller 是否可以使用它)

import xlrd
import pandas as pd
from os.path import join, isfile
from os import environ
if isfile(join(environ['USERPROFILE'],'Downloads','Report_15_13__12_12_2018.xlsx')):
rd=pd.Excelfile(join(environ['USERPROFILE'],'Downloads','Report_15_13__12_12_2018.xlsx'))
df1=rd.parse()
df1.to_excel(join(environ['USERPROFILE'],'Downloads','test.xls'))

我找不到关于如何克服的答案,但通过从 Pandas (https://github.com/pandas-dev/pandas/blob/v0.24.1/pandas/io/excel.py#L658-L718) 复制代码,我成功地解决了这个问题。对于我的情况,我只有一张纸,但我相信可以轻松支持多张纸。

from xlrd import (xldate, XL_CELL_DATE,
XL_CELL_ERROR, XL_CELL_BOOLEAN,
XL_CELL_NUMBER,open_workbook)

from datetime import date, datetime, time, timedelta
from pandas import DataFrame
from numpy import array,nan
from os import environ
from os.path import join

book=open_workbook(join(environ['USERPROFILE'],'Downloads','excel_to_read.xls'))
epoch1904 = book.datemode
sheet=book.sheet_by_index(0)
def _parse_cell(cell_contents, cell_typ):
"""converts the contents of the cell into a pandas
appropriate object"""

if cell_typ == XL_CELL_DATE:

# Use the newer xlrd datetime handling.
try:
cell_contents = xldate.xldate_as_datetime(
cell_contents, epoch1904)
except OverflowError:
return cell_contents

# Excel doesn't distinguish between dates and time,
# so we treat dates on the epoch as times only.
# Also, Excel supports 1900 and 1904 epochs.
year = (cell_contents.timetuple())[0:3]
if ((not epoch1904 and year == (1899, 12, 31)) or
(epoch1904 and year == (1904, 1, 1))):
cell_contents = time(cell_contents.hour,
cell_contents.minute,
cell_contents.second,
cell_contents.microsecond)

elif cell_typ == XL_CELL_ERROR:
cell_contents = nan
elif cell_typ == XL_CELL_BOOLEAN:
cell_contents = bool(cell_contents)
elif cell_typ == XL_CELL_NUMBER:
# GH5394 - Excel 'numbers' are always floats
# it's a minimal perf hit and less surprising
val = int(cell_contents)
if val == cell_contents:
cell_contents = val
return cell_contents

data = []


for i in range(sheet.nrows):
row = [_parse_cell(value, typ)
for value, typ in zip(sheet.row_values(i),
sheet.row_types(i))]
data.append(row)
NoOfColumns=len(sheet.row_values(i))
NoOfRows=sheet.nrows-1
DataFrame(array(data[1:]).reshape(NoOfRows,NoOfColumns),columns=data[0]).to_excel(join(environ['USERPROFILE'],'Desktop','test.xlsx'),index=False, engine='xlsxwriter')

关于python - 为什么 Pandas read_excel 函数在 Pyinstaller .exe 中返回错误,但在 Python 解释器下却没有?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52201870/

26 4 0
文章推荐: html - CSS div block 导致 WebKit 浏览器上的列表错位
文章推荐: linux - socket编程中如何获取receiver的接收接口(interface)信息?
文章推荐: html - CSS: space
  • 所以它们在固定高度的
  • Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com