在上一篇的基础上进一步改进,当时未考虑sheet插页在不同的excel表里或有或无的情况
主要就是引入了 while… try… except… 的机制
import numpy
as np
import pandas
as pd
import os
path
= 'E:\\工作台\\file\\'
os
.chdir
(path
)
'''
一人多笔插页的加载
'''
def get_excel(path
,sheet
):
i
= 0
datas
= []
path_lst
= os
.listdir
(path
)
while i
<= len(path_lst
):
try:
data
= pd
.read_excel
(path_lst
[i
],
sheet_name
=sheet1
,
header
=0,
sep
=',',
dtype
=np
.str)
datas
.append
(data
)
i
+=1
except Exception
as e
:
i
+=1
continue
return datas
sheet1
= '省份'
df1
= get_excel
(path
,sheet1
)
for i
in range(len(df1
)):
lst
= 'df1[%i]' % i
print(lst
,end
=',')
res_sheet1
= pd
.concat
([
df1
[0],df1
[1],df1
[2],df1
[3],df1
[4],df1
[5],df1
[6],df1
[7]
])
'''
其余插页,再一一赋值sheet变量,依次调用函数,依次获取分表的name标签用于最后的
合并拼接
'''
图片说明:path_lst 为excel文件列表,其中两个没有‘省份’的sheet插页,df1即是加载后dataframe列表集,res_sheet1为最后的数据合并集合。
欢迎各位大佬补充。
转载请注明原文地址: https://mac.8miu.com/read-496022.html