用python代码将文件夹下所有txt文件转换为excel
时间: 2024-05-06 22:21:02 浏览: 259
使用matlab或python将txt文件转为excel表格
Sure, 可以使用Python的pandas模块来实现,以下是示例代码:
```
import pandas as pd
import glob
# path to folder containing txt files
path = r'/path/to/folder/'
# get all txt files in folder
all_files = glob.glob(path + "*.txt")
# loop through each file, read data and append to list
txt_data = []
for file in all_files:
with open(file, 'r') as f:
data = f.read()
txt_data.append(data)
# create pandas dataframe from list of txt data
df = pd.DataFrame({'txt_data': txt_data})
# create excel file from pandas dataframe and save to folder
df.to_excel('output.xlsx', index=False)
```
这样,你就可以将文件夹下所有的txt文件转换为一个Excel文件。希望能对你有所帮助!
阅读全文