Python ValueError:对关闭的文件执行I/O操作以\u excel
我正在尝试对Python ValueError:对关闭的文件执行I/O操作以\u excel,python,pandas,Python,Pandas,我正在尝试对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\A
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.
作为此行中的错误:
join2.to_excel("shared_BM_NL_Q2_DNS.xlsx")
我还需要一些人建议我如何为这样的代码执行一个好的方式
我希望有人能帮助我如何解决这个错误最终我找到了解决方案,这是因为我解决了
xlsx
中行的限制,而不是将转换为excel
使用转换为csv
,如下代码:
join2.to_csv("shared_BM_NL_Q2_DNS.csv")
首先,您的代码可以通过使用
pd.read\u excel
将所有内容读入数据帧的dict中来大大简化,这些数据帧将由工作表名称键入。然后迭代dict并执行相同的操作(只允许编写一次)。但这并不能纠正错误。@Alolz非常感谢您的支持,您能给我举个例子吗?因为我有很多优秀的工作表:D
join2.to_csv("shared_BM_NL_Q2_DNS.csv")