08-05-2022, 06:41 PM
How to Convert Multiple Text Files to a Single CSV in Python?
5/5 – (1 vote)
You can merge multiple text files to a single CSV file in Python by using the expression to filter out all path names of text files in a given folder. Then iterate over all those path names and use the function to read the file contents and write append them to the CSV.
[i]Example: merge those files[/i]
Here’s the simple example:
The resulting output CSV file shows that all text files have been merged:
![[Image: image-2.png]](https://blog.finxter.com/wp-content/uploads/2022/08/image-2.png)
You can replace the separator (e.g., from single empty space to comma) by using the function before writing it in the CSV:
The resulting CSV is neatly separated with comma characters:
![[Image: image-3.png]](https://blog.finxter.com/wp-content/uploads/2022/08/image-3.png)
In case you need some more advanced ways to convert the text files to the CSV, you may want to check out the Pandas function to read the CSV into a DataFrame.
As soon as you have it as a DataFrame, you can do advanced processing such as merging, column selection, slicing, etc.
Related Tutorial: How to Read a CSV to a DataFrame?
5/5 – (1 vote)
You can merge multiple text files to a single CSV file in Python by using the
Code:
glob.glob('./*.txt')Code:
open()
[i]Example: merge those files[/i]Here’s the simple example:
Code:
import glob with open('my_file.csv', 'a') as csv_file: for path in glob.glob('./*.txt'): with open(path) as txt_file: txt = txt_file.read() + '\n' csv_file.write(txt)The resulting output CSV file shows that all text files have been merged:
![[Image: image-2.png]](https://blog.finxter.com/wp-content/uploads/2022/08/image-2.png)
You can replace the separator (e.g., from single empty space to comma) by using the
Code:
txt.replace(' ', ',')Code:
import glob with open('my_file.csv', 'a') as csv_file: for path in glob.glob('./*.txt'): with open(path) as txt_file: txt = txt_file.read() + '\n' txt = txt.replace(' ', ',') csv_file.write(txt)The resulting CSV is neatly separated with comma characters:
![[Image: image-3.png]](https://blog.finxter.com/wp-content/uploads/2022/08/image-3.png)
In case you need some more advanced ways to convert the text files to the CSV, you may want to check out the Pandas
Code:
read_csv()As soon as you have it as a DataFrame, you can do advanced processing such as merging, column selection, slicing, etc.
Related Tutorial: How to Read a CSV to a DataFrame?

