【问题标题】:python: writing data to excel file but "cannot close file"?python:将数据写入excel文件但“无法关闭文件”?
【发布时间】:2017-12-07 00:27:16
【问题描述】:

所以我有这个 Pandas 数据表,我希望将其写入 Excel 工作簿。 我正在使用 pyexcelerate 包。 这是我的代码:

from pyexcelerate import Workbook

def df_to_excel(df, path, sheet_name='Sheet 1'):
    data = [df.columns.tolist(), ] + df.values.tolist()
    wb = Workbook()
    wb.new_sheet(sheet_name, data=data)
    wb.save(path)

df_to_excel(df=MergedDataAll, path=merged_file)

我之前使用相同的代码时没有这个问题;但是今天这个错误一直弹出:

Traceback (most recent call last):
  File "C:/Users/y/Documents/python codes/DataMerging.py", line 71, in <module>
    df_to_excel(df=MergedDataAll, path=merged_file)
  File "C:/Users/y/Documents/python codes/DataMerging.py", line 68, in df_to_excel
    wb.save(path)
  File "C:\Users\y\AppData\Roaming\Python\Python36\site-packages\pyexcelerate\Workbook.py", line 82, in save
    self._save(fp)
  File "C:\Users\y\AppData\Roaming\Python\Python36\site-packages\pyexcelerate\Workbook.py", line 78, in _save
    self._writer.save(file_handle)
  File "C:\Users\y\AppData\Roaming\Python\Python36\site-packages\pyexcelerate\Writer.py", line 58, in save
    for s in sheetStream:
  File "C:\Users\y\AppData\Roaming\Python\Python36\site-packages\jinja2\environment.py", line 1045, in generate
    yield self.environment.handle_exception(exc_info, True)
  File "C:\Users\y\AppData\Roaming\Python\Python36\site-packages\jinja2\environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\y\AppData\Roaming\Python\Python36\site-packages\jinja2\_compat.py", line 37, in reraise
    raise value.with_traceback(tb)
  File "C:\Users\y\AppData\Roaming\Python\Python36\site-packages\pyexcelerate\templates\xl\worksheets\sheet.xml", line 11, in top-level template code
    <sheetData>{% for x, row in worksheet.get_xml_data() %}{{ worksheet.get_row_xml_string(x) }}{% for cell in row %}{{ cell }}{% endfor %}</row>{% endfor %}</sheetData>
  File "C:\Users\y\AppData\Roaming\Python\Python36\site-packages\pyexcelerate\Worksheet.py", line 265, in get_xml_data
    row_data.append(self.__get_cell_data(cell, x, y, style))
  File "C:\Users\y\AppData\Roaming\Python\Python36\site-packages\pyexcelerate\Worksheet.py", line 188, in __get_cell_data
    z = '"><v>%s</v></c>' % (DataTypes.to_excel_date(cell))
  File "C:\Users\y\AppData\Roaming\Python\Python36\site-packages\pyexcelerate\DataTypes.py", line 57, in to_excel_date
    excel_date = delta.days + (float(delta.seconds) + float(delta.microseconds) / 1E6) / (60 * 60 * 24) + 1
AttributeError: 'NaTType' object has no attribute 'days'
Exception ignored in: <bound method ZipFile.__del__ of <zipfile.ZipFile [closed]>>
Traceback (most recent call last):
  File "C:\Users\y\AppData\Local\Programs\Python\Python36-32\lib\zipfile.py", line 1663, in __del__
  File "C:\Users\y\AppData\Local\Programs\Python\Python36-32\lib\zipfile.py", line 1680, in close
ValueError: seek of closed file

我尝试在谷歌上搜索,并使用了类似于

的东西
wd.close()
wd._archive_.close()

但两者都没有用。

这是我研究过的一些参考帖子:

Openpyxl does not close Excel workbook in read only mode

Python to close a workbook using win32com

Closing files in openpyxl

https://github.com/python-pillow/Pillow/issues/1630

【问题讨论】:

  • 我认为你搞错了——你正试图寻找一个已经关闭的文件。 merged_file是什么,在进入这个功能之前是否已经关闭?
  • 我不确定确定我正在正确读取异常,但我认为您遇到的第一个“真正”错误不是 zip 文件搜索问题,而是而是AttributeError: 'NaTType' object has no attribute 'days',而代码正在尝试写出日期。你能检查你的数据,并确保你没有在应该是日期的地方有虚假值(或者即使它意味着其他东西也可能被误认为是日期)?
  • @Shadow the mapped_file 是我正在写这个文件的文件名的文件路径
  • @Blckknght 我有点困惑......你是说我有一列应该是日期但不是?还是相反,因为它不是日期,但它正在尝试将数据写为日期?
  • 我猜您的行中该列的值无效。快速谷歌显示NaTType 是 Pandas 用于“不是时间”的类型(相当于数字字段中的 NaN(非数字))。您可能需要将该值转换为其他值,或过滤掉该行,因为看起来 excel 翻译代码无法自行处理它。

标签: python excel pyexcelerate


【解决方案1】:

我终于明白了!所以我想我不妨分享我的发现。

答案来自这篇文章:

'NaTType' object has no attribute 'days'

升级pandas库基本解决了这个问题...

pip install --upgrade pandas

特别感谢 Blckknght 指出 NaTType 问题;如果他没有指出,我不会对此进行更多研究。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-31
    • 1970-01-01
    • 1970-01-01
    • 2023-02-24
    • 1970-01-01
    • 1970-01-01
    • 2014-03-13
    相关资源
    最近更新 更多