I have many .txt files. I want to convert a few files ending with specific names into csv and combine them into one csv.
### Folder Name: text_files
python_gramm.py
aadd01.txt
aaxx02.txt
aaff03.txt
hhdd01.txt
attd02.txt
auud03.txt
.
.
A text file contains columns of data as given below:
I would like to convert only those .txt
files having name '___01.txt' to csv format and combine them
My code
#### python_gramm.py
import os
import globos.chdir('text_files/')extension = 'txt'all_filenames = [i for i in glob.glob('*01.{}'.format(extension))]#combine all files in the listcombined_csv = pd.concat([pd.read_csv(f, delimiter='\t') for f in all_filenames ])#export to csvcombined_csv.to_csv( "combined_csv.csv", index=False, encoding='utf-8-sig')
Above code ran successfully without errors.
print(combined_csv)
0 4.18890 9.325750 ... 2.438860e-05 4.100250e+04
1 4.21399 9.339870 ... 2.461170e-05 4.063090e+04
2 4.26399 9.341690 ... 2.451400e-05 4.079280e+04
3 4.80021 9.346420 ... 2.442850e-05 4.093570e+04
4 6.50219 9.339300 ... 2.432250e-05 4.111400e+04