python读取表头——对比sql字段

大量文件需要写入数据库,时间跨度比较大,部分字段略作修改了
怎么对比各个文件的表头和sql字段是否一致
肯定不能一个一个打开去看,太麻烦了,而且上百个上千个文件呢
下面就是今天的小技巧,遍历访问文件,提取出表头

import pandas  as pd
import numpy as np
import os,re
path=r'xxxxxxxxx'
wj=os.listdir(path)
#bt_path=r"xxxxx\headers.xlsx"
#xh=pd.read_excel(bt_path) #日期匹配header
result=[]
for wj_name in wj:
    data_path=path+'\\'+wj_name
    #start_num=int(re.search('-20',wj_name).span()[1])
    #date=wj_name[start_num-2:start_num+8]
    #hd=int(xh[xh['日期']==date]['header'])#日期匹配header
    data=pd.read_excel(data_path,header=hd)
    col=list(data.columns)
    col.insert(0,date)
    result.append(col)
new_data=pd.DataFrame(result)
new_data.to_excel('表头.xlsx',index=False)

注释掉的代码是为了解决

不同日期,表头位置header不同

问题的

如果觉得对你有帮助!麻烦点个赞再走

发布了35 篇原创文章 · 获赞 35 · 访问量 2615

猜你喜欢

转载自blog.csdn.net/qq_35866846/article/details/102632294