拼接txt文件并输出到excel

# 法1:pandas.read_csv(path,engine='python')
#之所以要加engine='python' 是因为路径里有中文,详情参考文末参考文章
path_folder = ""
path_output = "“”
df_all = pd.DataFrame()
for paths in os.walk(path_folder):
    for path in paths[2]:
        filepath = path_folder + '\\'+str(path)
        df = pd.read_csv(filepath,sep='\t',header=None,engine='python') 
        df_all = df_all.append(df)
        
columns =['A','B','C','D']
df_all.columns=  columns     
df_all.to_excel(path_output,index=False)
# 法2:
classes_path = ""
with open(classes_path) as f:
    text_ori = f.readlines()# 全部读取最后返回一个列表存所有的类,每行后面都会带有“\n”
    print(type(text_ori))
    print(text_ori[0])
#     print(text)
    f.close()  
# 遍历,去除首尾的空白(包括'\n', '\r',  '\t',  ' '),并按照'\t'分割 
# 因为读出来的行是这样的  1	8	8	20190101,中间的空格是\t 制表符

text = [line.strip().split('\t') for line in text_ori]
#若需要连接 则直接join
# text2= ["".join(line.strip().split('\t')) for line in text_ori]
columns =['a','b','c','d']
df_all_2 = pd.DataFrame(text,columns=columns)
df_all2.to_excel(path_output,index=False)

参考:
pandas.read_csv() 报错 OSError: Initializing from file failed

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值