gpt4 book ai didi

python - 值错误 : I/O operation on closed file to_excel pandas

转载 作者:行者123 更新时间:2023-12-04 04:01:32 25 4
gpt4 key购买 nike

我正在尝试 concat床单 xlsx文件,我发现以下错误:

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")
我还需要有人建议我如何为这样的代码执行一种好方法
我希望有人能帮助我如何解决这个错误

最佳答案

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

join2.to_csv("shared_BM_NL_Q2_DNS.csv")

关于python - 值错误 : I/O operation on closed file to_excel pandas,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62988232/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com