[英]How to read multiple text files from a directory, convert them all to excel files
I have 10s of tab delimeted text files in my local directory.我的本地目录中有 10 个制表符分隔的文本文件。 When I copy and paste a text file into an excel sheet, it becomes a file having 100s of columns.
当我将文本文件复制并粘贴到 Excel 工作表中时,它变成了一个包含 100 列的文件。 Now, I would like to read all the text files and convert them to corresponding excel files.
现在,我想读取所有文本文件并将它们转换为相应的 excel 文件。
If there was a single file, I would have done the following way:如果只有一个文件,我会这样做:
import pandas as pd
df = pd.read_csv("H:\\Yugeen\\text1.txt", sep='\t')
df.to_excel('H:\\Yugeen\\output1.xlsx', 'Sheet1', index = False)
Is there any way to achive a solution that I am looking for ?有什么方法可以实现我正在寻找的解决方案吗?
I use this function to list all files in a directory, along with their file path:我使用此函数列出目录中的所有文件及其文件路径:
import os
def list_files_in_directory(path):
'''docstring for list_files_in_directory'''
x = []
for root, dirs, files in os.walk('.'+path):
for file in files:
x.append(root+'/'+file)
return x
Selecting for only text files:仅选择文本文件:
files = list_files_in_directory('.')
filtered_files = [i for i in files if '.txt' in i]
Like Sophia demonstrated, you can use pandas to create a dataframe.就像 Sophia 演示的那样,您可以使用 Pandas 创建一个数据框。 I'm assuming you want to merge these files as well.
我假设您也想合并这些文件。
import pandas as pd
dfs = []
for file in filtered_files:
df = pd.read_csv(file,sep='\t')
dfs.append(df)
df_master = pd.concat(dfs,axis=1)
filename = 'master_dataframe.csv'
df_master.to_csv(filename,index=False)
The saved file can then be opened in Excel.然后可以在 Excel 中打开保存的文件。
Are you talking about how to get the filenames?您是在谈论如何获取文件名吗? You can use the
glob
library.您可以使用
glob
库。
import glob
import pandas as pd
file_paths = glob.glob('your-directory\\*.txt')
for file in file_path:
df = pd.read_csv(file,sep='\t')
df.to_excel('output-directory\\filename.xlsx',index=False)
Does this answer your question?这回答了你的问题了吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.