python读取文件夹下txt文件,并转化成excel

import os
import pandas as pd
root = input("请输入文件夹路径:")
files = os.listdir(root)
cunchu_pos = root + '\\' + 'data'
os.mkdir(root + '\\' + 'data')
for file in files:
    position = root + '\\' + file
    print(position)
    # print(os.path.split(position)[-1].split(".")[0])
    with open(position, mode='r', encoding='utf-8')as f:
        data = f.readlines()
    li = [each.split() for each in data]
    # print(li)
    df_data = pd.DataFrame(li)
    cunchu = cunchu_pos + '\\' + os.path.split(position)[-1].split(".")[0] + '.xlsx'
    print(df_data )
    # 如果要保存每一列列名,header设置为True
    df_data.to_excel(cunchu, index=False, header=False)
python3: input('please input any key to exit')

数据格式是:每行用空格隔开的数据 整齐

你可能感兴趣的:(python,pandas,开发语言)