简体   繁体   English

如何从一个目录中读取多个文本文件,将它们全部转换为excel文件

[英]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.

相关问题 Python脚本读取一个目录中的多个excel文件并将它们转换为另一个目录中的.csv文件 - Python script to read multiple excel files in one directory and convert them to .csv files in another directory 如何从一个目录中读取多个文件并在 python 中读取 append 它们? - How to read multiple files from a directory and append them in python? 从目录中读取所有Excel文件,而不是单独列出它们 - Reading all excel files from a directory instead of listing them individually 从目录中读取多个图像并将其转换为.csv文件 - Read multiple images from a directory and turn them into .csv files 如何将目录的所有json文件转换为python中的文本文件? - how to convert all json files of directory to text files in python? 从多个 excel 文件中读取并将它们插入到 PosgreSQL DB 中的表中 - Read from multiple excel files and Insert them into a table in PosgreSQL DB 如何使用tesseract python 3读取目录中的所有pdf文件并转换为文本文件? - How to read all pdf files in a directory and convert to text file using tesseract python 3? 如何读取多个 csv 文件并将它们转换为一个 3d dataframe - How read multiple csv files and convert them to a 3d dataframe 如何从单个目录中读取多个csv文件并在Python中单独绘制它们? - How can I read multiple csv files from a single directory and graph them separately in Python? 如何读取一个目录下的所有excel个文件为pandas dataframe - How to read all excel files under a directory as a pandas dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM