首页 > 解决方案 > python读取excel文件不正确

问题描述

我有一个 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 = []

标签: pythonexcelpandas

解决方案


推荐阅读