利用Python读取文件列表并写入Excel
利用Python可以很方便读取列表,并写入相关文件。
import os
import pandas as pd
file_path = "C:/Users/Administrator/Desktop/test"
df = pd.Series(os.listdir(file_path))
fd = df.str.split('.txt', n=-1, expand=True) #split分割
fn = df.str.extract('(\d+)', expand=False) #extract正则
final_pd = pd.concat([fn, fd], axis=1) #利用concat连接series
final_pd.columns = ['所属品类', '文件名', ''] #定义列名
final_pd.to_excel('20210810PP.xlsx', index=False)
#print(final_pd)
参考链接:
https://zhuanlan.zhihu.com/p/93240054
https://pandas.pydata.org/docs/reference/frame.html
https://www.codenong.com/18062135/
https://blog.csdn.net/IMW_MG/article/details/78705359
https://www.runoob.com/python/python-reg-expressions.html
https://blog.csdn.net/weixin_43876801/article/details/102896570