用python代码将文件夹下所有txt文件转换为excel
时间: 2024-05-06 14:21:02 浏览: 21
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文件。希望能对你有所帮助!
相关问题
读取文件夹下所有txt文件并将数据逐列写进excel表格
你可以使用Python的第三方库pandas和os来实现读取文件夹中所有txt文件并将数据逐列写进Excel表格的操作。以下是一个示例代码:
```python
import pandas as pd
import os
# 定义一个空的DataFrame用于存放所有txt文件中的数据
df = pd.DataFrame()
# 获取当前文件夹下所有txt文件的文件名
files = [f for f in os.listdir('.') if os.path.isfile(f) and f.endswith('.txt')]
# 遍历所有txt文件
for file in files:
# 读取txt文件中的数据
with open(file, 'r') as f:
data = f.readlines()
# 按行分割数据
data = [line.strip().split(',') for line in data]
# 将数据转换为DataFrame格式
temp_df = pd.DataFrame(data)
# 将每个txt文件中的数据追加到总的DataFrame中
df = pd.concat([df, temp_df], axis=1)
# 将数据写入Excel文件
writer = pd.ExcelWriter('example.xlsx')
df.to_excel(writer, index=False)
writer.save()
```
其中,`example.xlsx`是要写入的Excel文件名。这段代码会读取当前文件夹下所有txt文件中的数据,并将数据逐列写入到Excel表格中。你可以根据实际需求修改这些参数。
Python读取路径下文件夹中多级文件夹内的txt文件合并成一个txt文件,在转换成excel
以下是示例代码:
```python
import os
import pandas as pd
# 遍历文件夹,获取所有txt文件路径
def get_txt_files(folder_path):
files = []
for root, dirs, filenames in os.walk(folder_path):
for filename in filenames:
if filename.endswith(".txt"):
files.append(os.path.abspath(os.path.join(root, filename)))
return files
# 将多个txt文件内容合并为一个字符串
def combine_txt_files(txt_files):
content = ""
for txt_file in txt_files:
with open(txt_file, "r") as f:
content += f.read()
return content
# 将合并后的内容写入到新文件中
def write_combined_content_to_file(content, file_path):
with open(file_path, "w") as f:
f.write(content)
# 将txt文件转换为excel文件
def txt_to_excel(txt_file, excel_file):
df = pd.read_csv(txt_file, delimiter="\t", header=None) # 这里假设txt文件以tab分隔符分隔,没有表头
df.to_excel(excel_file, index=False, header=None) # 写入excel文件,不包含行索引和表头
if __name__ == "__main__":
folder_path = "/path/to/folder" # 文件夹路径
txt_files = get_txt_files(folder_path) # 获取txt文件列表
combined_content = combine_txt_files(txt_files) # 合并所有txt文件内容
combined_file_path = "/path/to/combined/file.txt" # 合并后的文件路径
write_combined_content_to_file(combined_content, combined_file_path) # 将合并后的内容写入到新文件
excel_file_path = "/path/to/converted/file.xlsx" # 转换后的excel文件路径
txt_to_excel(combined_file_path, excel_file_path) # 将txt文件转换为excel文件
```
说明:
1. `get_txt_files`函数用于遍历文件夹,获取所有后缀名为`.txt`的文件路径,使用了`os.walk()`方法;
2. `combine_txt_files`函数将多个txt文件内容合并为一个字符串;
3. `write_combined_content_to_file`函数将合并后的内容写入到新文件中;
4. `txt_to_excel`函数将单个txt文件转换为excel文件,使用了`pandas`库,假设txt文件以tab分隔符分隔,并且没有表头;
5. 在`main`函数中,首先获取所有txt文件路径,然后合并所有txt文件内容,并写入到新文件中,最后将新文件转换为excel文件。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)