import pandas as pd
filePath = r'C:\\Users\Administrator\Desktop\\21aa.xlsx'
dfL = [aF,bF,cF,dF,eF]
sheetL = ['a','b','c','d']
vL = ['av','bv','cv','dv']
writer = pd.ExcelWriter(filePath)
for i,j in zip(dfL,sheetL):
i.to_excel(writer,j,index=False)
writer.save()
writer.close()
for i,j in zip(sheetL,vL):
exec ("%s = pd.read_excel('%s',sheet_name='%s',engine='openpyxl')"%(i,filePath,j))
成功地读取文件并赋值。注意filePath 中的\U和\2必须在前面再多加\ 来转义(\U,\2就是U,2),套入exec 语句中要加’’,变成’%s’,少任何一个环节都会报错(至少Windows上用exec()要这样)。
说明一下,to_excel 写入Excel是有问题的,打开Excel会显示要修复。这个代码我后续再想办法优化,如果有高人指点一下也感谢了。