ValueError:对关闭的文件执行I/O操作以\u excel

2024-05-12 20:22:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在xlsx文件中concat个工作表,发现以下错误:

Traceback (most recent call last):
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python38-32\lib\xml\etree\ElementTree.py", line 832, in _get_writer
    yield file.write
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python38-32\lib\xml\etree\ElementTree.py", line 772, in write
    serialize(write, self._root, qnames, namespaces,
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python38-32\lib\xml\etree\ElementTree.py", line 937, in _serialize_xml
    _serialize_xml(write, e, qnames, None,
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python38-32\lib\xml\etree\ElementTree.py", line 937, in _serialize_xml
    _serialize_xml(write, e, qnames, None,
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python38-32\lib\xml\etree\ElementTree.py", line 937, in _serialize_xml
    _serialize_xml(write, e, qnames, None,
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python38-32\lib\xml\etree\ElementTree.py", line 931, in _serialize_xml
    write(" %s=\"%s\"" % (qnames[k], v))
MemoryError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:/Python Projects/MyAuditPy/pd_read.py", line 265, in <module>
    join2.to_excel("shared_BM_NL_Q2_DNS.xlsx")
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\generic.py", line 2175, in to_excel
    formatter.write(
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\io\formats\excel.py", line 738, in write
    writer.save()
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\io\excel\_openpyxl.py", line 43, in save
    return self.book.save(self.path)
  File "D:\Python Projects\MyAuditPy\venv\lib\site-packages\openpyxl\workbook\workbook.py", line 392, in save
    save_workbook(self, filename)
  File "D:\Python Projects\MyAuditPy\venv\lib\site-packages\openpyxl\writer\excel.py", line 293, in save_workbook
    writer.save()
  File "D:\Python Projects\MyAuditPy\venv\lib\site-packages\openpyxl\writer\excel.py", line 275, in save
    self.write_data()
  File "D:\Python Projects\MyAuditPy\venv\lib\site-packages\openpyxl\writer\excel.py", line 75, in write_data
    self._write_worksheets()
  File "D:\Python Projects\MyAuditPy\venv\lib\site-packages\openpyxl\writer\excel.py", line 215, in _write_worksheets
    self.write_worksheet(ws)
  File "D:\Python Projects\MyAuditPy\venv\lib\site-packages\openpyxl\writer\excel.py", line 200, in write_worksheet
    writer.write()
  File "D:\Python Projects\MyAuditPy\venv\lib\site-packages\openpyxl\worksheet\_writer.py", line 360, in write
    self.close()
  File "D:\Python Projects\MyAuditPy\venv\lib\site-packages\openpyxl\worksheet\_writer.py", line 368, in close
    self.xf.close()
  File "D:\Python Projects\MyAuditPy\venv\lib\site-packages\openpyxl\worksheet\_writer.py", line 299, in get_stream
    pass
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python38-32\lib\contextlib.py", line 120, in __exit__
    next(self.gen)
  File "D:\Python Projects\MyAuditPy\venv\lib\site-packages\et_xmlfile\xmlfile.py", line 50, in element
    self._write_element(el)
  File "D:\Python Projects\MyAuditPy\venv\lib\site-packages\et_xmlfile\xmlfile.py", line 77, in _write_element
    xml = tostring(element)
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python38-32\lib\xml\etree\ElementTree.py", line 1133, in tostring
    ElementTree(element).write(stream, encoding,
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python38-32\lib\xml\etree\ElementTree.py", line 772, in write
    serialize(write, self._root, qnames, namespaces,
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python38-32\lib\contextlib.py", line 131, in __exit__
    self.gen.throw(type, value, traceback)
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python38-32\lib\xml\etree\ElementTree.py", line 832, in _get_writer
    yield file.write
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python38-32\lib\contextlib.py", line 525, in __exit__
    raise exc_details[1]
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python38-32\lib\contextlib.py", line 510, in __exit__
    if cb(*exc_details):
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python38-32\lib\contextlib.py", line 382, in _exit_wrapper
    callback(*args, **kwds)
ValueError: I/O operation on closed file.

这是我的相关代码:

import os
import pandas as pd

shared_BM_NL_Q2_DNS = 'Shared_BM_NL_Q2_DNS.xlsx'

shared_BM_NL_Q2_DNS_df1 = pd.read_excel(os.path.join(os.path.dirname(__file__), shared_BM_NL_Q2_DNS), sheet_name='client31_KPN')
shared_BM_NL_Q2_DNS_df2 = pd.read_excel(os.path.join(os.path.dirname(__file__), shared_BM_NL_Q2_DNS), sheet_name='client32_T-Mobile')
shared_BM_NL_Q2_DNS_df3 = pd.read_excel(os.path.join(os.path.dirname(__file__), shared_BM_NL_Q2_DNS), sheet_name='client33_Vodafone')

#shared_BM_NL_Q2_DNS
shared_BM_NL_Q2_DNS_df1.columns = shared_BM_NL_Q2_DNS_df1.columns.str.replace(' ', '_')
shared_BM_NL_Q2_DNS_df1.columns = shared_BM_NL_Q2_DNS_df1.columns.str.replace('\n', '')
shared_BM_NL_Q2_DNS_df1.columns = shared_BM_NL_Q2_DNS_df1.columns.str.replace(r"[^a-zA-Z\d\_]+", "")
shared_BM_NL_Q2_DNS_df1.columns = map(str.lower, shared_BM_NL_Q2_DNS_df1.columns)

shared_BM_NL_Q2_DNS_df2.columns = shared_BM_NL_Q2_DNS_df2.columns.str.replace(' ', '_')
shared_BM_NL_Q2_DNS_df2.columns = shared_BM_NL_Q2_DNS_df2.columns.str.replace('\n', '')
shared_BM_NL_Q2_DNS_df2.columns = shared_BM_NL_Q2_DNS_df2.columns.str.replace(r"[^a-zA-Z\d\_]+", "")
shared_BM_NL_Q2_DNS_df2.columns = map(str.lower, shared_BM_NL_Q2_DNS_df2.columns)

shared_BM_NL_Q2_DNS_df3.columns = shared_BM_NL_Q2_DNS_df3.columns.str.replace(' ', '_')
shared_BM_NL_Q2_DNS_df3.columns = shared_BM_NL_Q2_DNS_df3.columns.str.replace('\n', '')
shared_BM_NL_Q2_DNS_df3.columns = shared_BM_NL_Q2_DNS_df3.columns.str.replace(r"[^a-zA-Z\d\_]+", "")
shared_BM_NL_Q2_DNS_df3.columns = map(str.lower, shared_BM_NL_Q2_DNS_df3.columns)
dataframes2 = [shared_BM_NL_Q2_DNS_df1, shared_BM_NL_Q2_DNS_df2, shared_BM_NL_Q2_DNS_df3]
join2 = pd.concat(dataframes2).reset_index(drop=True)

join2.to_excel("shared_BM_NL_Q2_DNS.xlsx")

作为此行中的错误:

join2.to_excel("shared_BM_NL_Q2_DNS.xlsx")

我还需要一些人建议我如何为这样的代码执行一个好的方式

我希望有人能帮助我如何解决这个错误


Tags: columnsinpydnslibnllineusers
1条回答
网友
1楼 · 发布于 2024-05-12 20:22:42

最后,我找到了解决方案,因为我解决了xlsx中的行限制,而不是转换to_excel是使用to_csv作为以下代码:

join2.to_csv("shared_BM_NL_Q2_DNS.csv")

相关问题 更多 >