python 合并一个文件夹下的多个txt文件,并对文本内容分列。
原始文件:
最终结果:
import pandas as pd
import xlwt
import pandas as pd
from sqlalchemy import create_engine
import pandas as pd
import os
import glob
dirPath = glob.iglob(r'C:\Users\18277\Desktop\权益')#读取装有合并数据表的文件夹名称
for big_file in dirPath:# 获取文件夹下的文件名并赋值给 filesfiles = os.listdir(big_file) b=[]
for i in files:with open(r'C:\Users\18277\Desktop\权益\{}'.format(i)) as f:lines = f.readlines() #读取文件for j in range(len(lines)):#循环读取test=lines[j].split("|")# 分列test_=pd.DataFrame(test).T#转置成行b.append(test_)qy_result=pd.DataFrame(pd.concat(b)) #合并数据
qy_result.columns = ["order_no","qy_amount","recive_time","loan_amount","term",
"rate","laon_time","lkt_no","lkt_origin_no","lkt_pay_no","uploan_time"] #重命名