Converting txt to CSV separated by column

79 views Asked by At

I have a folder with multiply .txt files in the all in the same format, tab separated. I'm trying to convert them to csv's separated by column.

I've tried a simple read_file.to_csv (r'C:\Users\Desktop\workspace\Converter\20200923.csv', index=False)

But it doesn't do the separation I'm looking for. Any suggestions are most welcomed. Thank you!

1

There are 1 answers

4
mullinscr On BEST ANSWER

Try something like this:

import os
import pandas as pd

for filename in os.listdir('path/to/dir/'):
    if filename.endswith('.txt'):
        df = pd.read_table(filename,sep='\t', header=None) # header=None becuase you didn't say that it was data, if it is data just remove this.
        df.to_csv(f'{filename[:-3]}csv', index=False)