错误提示:ValueError: I/O operation on closed file.,是啥原因?

错误提示:ValueError: I/O operation on closed file.,是啥原因?

elif shu=='3':

slie='配送数量'

wb=openpyxl.load_workbook(path)

wb.save(path)

sheet_name="sheet0"

all_dict = {}
all_num = 0
num = 0

content = pd.read_excel(path, sheet_name=sheet_name)

print(content.head())

data = content[['业务日期', '企业编码', '企业名称', '商品编码', slie]]

for x in data.itertuples():

all_num += x[4]

keys = x[1] + str(x[2]) + str(x[3])

if keys not in all_dict:

# print(x)

all_dict[keys] = {

"业务日期": x[1],

"企业编码": x[2],

'企业名称': x[3],

'商品编码': x[4],

slie: x[5]

}

else:

all_dict[keys][slie] += x[5]

result = [x for x in all_dict.values()]
pf = pd.DataFrame(result)
order = ["业务日期", "企业编码", '企业名称','商品编码', slie] # 指定列的顺序
pf = pf[order]
file_path = pd.ExcelWriter(path) # 打开excel文件

替换空单元格

输出

print('解析数据并保存数据')
pf.to_excel(file_path, encoding='utf-8', index=False, sheet_name="sheet1")
file_path.save()
print('数据处理完毕')
这是代码,之前运行都好好的,今天突然出了问题,ValueError: I/O operation on closed file.
以下是错误代码:
《门店配送-202107.xlsx》-下载成功
数据处理中请稍后……………………
C:\Users\ylei4\AppData\Local\Programs\Python\Python37-32\lib\site-packages\openpyxl\styles\stylesheet.py:221: UserWarning: Workbook contains no default style, apply openpyxl's default
warn("Workbook contains no default style, apply openpyxl's default")
Traceback (most recent call last):
File "C:\Users\ylei4\AppData\Local\Programs\Python\Python37-32\lib\xml\etree\ElementTree.py", line 836, in _get_writer

yield file.write

File "C:\Users\ylei4\AppData\Local\Programs\Python\Python37-32\lib\xml\etree\ElementTree.py", line 777, in write

short_empty_elements=short_empty_elements)

File "C:\Users\ylei4\AppData\Local\Programs\Python\Python37-32\lib\xml\etree\ElementTree.py", line 942, in _serialize_xml

short_empty_elements=short_empty_elements)

File "C:\Users\ylei4\AppData\Local\Programs\Python\Python37-32\lib\xml\etree\ElementTree.py", line 942, in _serialize_xml

short_empty_elements=short_empty_elements)

File "C:\Users\ylei4\AppData\Local\Programs\Python\Python37-32\lib\xml\etree\ElementTree.py", line 942, in _serialize_xml

short_empty_elements=short_empty_elements)

File "C:\Users\ylei4\AppData\Local\Programs\Python\Python37-32\lib\xml\etree\ElementTree.py", line 935, 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 "c:/Users/ylei4/OneDrive - JNJ/Desktop/配送脚本/高济大药房v1.py", line 105, in <module>

wb.save(path)

File "C:\Users\ylei4\AppData\Local\Programs\Python\Python37-32\lib\site-packages\openpyxl\workbook\workbook.py", line 407, in save

save_workbook(self, filename)

File "C:\Users\ylei4\AppData\Local\Programs\Python\Python37-32\lib\site-packages\openpyxl\writer\excel.py", line 293, in save_workbook

writer.save()

File "C:\Users\ylei4\AppData\Local\Programs\Python\Python37-32\lib\site-packages\openpyxl\writer\excel.py", line 275, in save

self.write_data()

File "C:\Users\ylei4\AppData\Local\Programs\Python\Python37-32\lib\site-packages\openpyxl\writer\excel.py", line 75, in write_data

self._write_worksheets()

File "C:\Users\ylei4\AppData\Local\Programs\Python\Python37-32\lib\site-packages\openpyxl\writer\excel.py", line 215, in _write_worksheets

self.write_worksheet(ws)

File "C:\Users\ylei4\AppData\Local\Programs\Python\Python37-32\lib\site-packages\openpyxl\writer\excel.py", line 200, in write_worksheet

writer.write()

File "C:\Users\ylei4\AppData\Local\Programs\Python\Python37-32\lib\site-packages\openpyxl\worksheet\_writer.py", line 361, in write

self.close()

File "C:\Users\ylei4\AppData\Local\Programs\Python\Python37-32\lib\site-packages\openpyxl\worksheet\_writer.py", line 369, in close

self.xf.close()

File "C:\Users\ylei4\AppData\Local\Programs\Python\Python37-32\lib\site-packages\openpyxl\worksheet\_writer.py", line 300, in get_stream

pass

File "C:\Users\ylei4\AppData\Local\Programs\Python\Python37-32\lib\contextlib.py", line 119, in exit

next(self.gen)

File "C:\Users\ylei4\AppData\Local\Programs\Python\Python37-32\lib\site-packages\et_xmlfile\xmlfile.py", line 50, in element

self._write_element(el)

File "C:\Users\ylei4\AppData\Local\Programs\Python\Python37-32\lib\site-packages\et_xmlfile\xmlfile.py", line 77, in _write_element

xml = tostring(element)

File "C:\Users\ylei4\AppData\Local\Programs\Python\Python37-32\lib\xml\etree\ElementTree.py", line 1136, in tostring

short_empty_elements=short_empty_elements)

File "C:\Users\ylei4\AppData\Local\Programs\Python\Python37-32\lib\xml\etree\ElementTree.py", line 777, in write

short_empty_elements=short_empty_elements)

File "C:\Users\ylei4\AppData\Local\Programs\Python\Python37-32\lib\contextlib.py", line 130, in exit

self.gen.throw(type, value, traceback)

File "C:\Users\ylei4\AppData\Local\Programs\Python\Python37-32\lib\xml\etree\ElementTree.py", line 836, in _get_writer

yield file.write

File "C:\Users\ylei4\AppData\Local\Programs\Python\Python37-32\lib\contextlib.py", line 524, in exit

raise exc_details[1]

File "C:\Users\ylei4\AppData\Local\Programs\Python\Python37-32\lib\contextlib.py", line 509, in exit

if cb(*exc_details):

File "C:\Users\ylei4\AppData\Local\Programs\Python\Python37-32\lib\contextlib.py", line 384, in _exit_wrapper

callback(*args, **kwds)

ValueError: I/O operation on closed file.

有大神帮忙看看吗?救救孩子吧

以上是 错误提示:ValueError: I/O operation on closed file.,是啥原因? 的全部内容, 来源链接: utcz.com/p/938032.html

回到顶部