0

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 ?

ychaulagain
  • 73
  • 1
  • 8

2 Answers2

1

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.

willwrighteng
  • 1,411
  • 11
  • 25
0

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?