简体   繁体   中英

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. When I copy and paste a text file into an excel sheet, it becomes a file having 100s of columns. Now, I would like to read all the text files and convert them to corresponding excel files.

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. 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.

Are you talking about how to get the filenames? You can use the glob library.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM