python错误读取excel文件

2024-04-19 15:47:41 发布

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

我有一个python文件,它使用pandas从工作簿中读取数据,并对数据执行若干操作,然后将数据写回单独工作表下的同一个文件。在程序中的一个循环中,我更新了一个写入excel文件的指针值,用于更新下一次迭代中使用的数据值。但是,当python读取下一组数据值(这些值是使用引用公式生成的)时,它无法识别这些值在迭代之间是否已更新,并将这些值读取为“nan”。如何确保python读取实际更新的值而不是“nan”?你知道吗

我在这里提供了代码片段的副本:

file = 'NS.xlsx'
x1 = pd.ExcelFile(file)

df1 = pd.read_excel(file,sheet_name = 'ReadtoPythonTemplate',skipfooter = 
12)
...operations...
book = load_workbook('NS.xlsx')
writer = pd.ExcelWriter('NS.xlsx', engine='openpyxl')
writer.book = book
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)

print(iteration_index)
iteration_index += 1
update_index = iteration_index + 3 #update pointer in excel file to generate
#new data set in the file to be read in again in the next iteration.

df8 = pd.DataFrame({'Index':[update_index]})
df8.to_excel(writer,"ReadtoPythonTemplate",startrow = 5,startcol = 1,index = 
False)
writer.save()
file = []

Tags: 文件to数据inindexwsupdatexlsx