Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Convert Multiple Text Files to a Single CSV in Python?

#1
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 glob.glob('./*.txt') expression to filter out all path names of text files in a given folder. Then iterate over all those path names and use the open() function to read the file contents and write append them to the CSV.

Example: merge those files

Here’s the simple example:

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:


You can replace the separator (e.g., from single empty space to comma) by using the txt.replace(' ', ',') function before writing it in the CSV:

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:


In case you need some more advanced ways to convert the text files to the CSV, you may want to check out the Pandas read_csv() 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?



https://www.sickgaming.net/blog/2022/08/...in-python/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] How to Convert MIDI to MP3 in Python – A Quick Overview xSicKxBot 0 2,394 09-02-2023, 02:04 PM
Last Post: xSicKxBot
  [Tut] Python zip(): Get Elements from Multiple Lists xSicKxBot 0 1,968 08-26-2023, 01:28 AM
Last Post: xSicKxBot
  [Tut] Write a Long String on Multiple Lines in Python xSicKxBot 0 1,500 08-17-2023, 11:05 AM
Last Post: xSicKxBot
  [Tut] Python Get All TXT Files in a Folder xSicKxBot 0 1,304 05-21-2023, 10:15 AM
Last Post: xSicKxBot
  [Tut] How to Access Multiple Matches of a Regex Group in Python? xSicKxBot 0 1,486 04-04-2023, 02:26 PM
Last Post: xSicKxBot
  [Tut] 5 Easy Ways to Edit a Text File From Command Line (Windows) xSicKxBot 0 1,318 03-05-2023, 08:32 AM
Last Post: xSicKxBot
  [Tut] Python Video to Text – Speech Recognition xSicKxBot 0 1,144 01-23-2023, 02:55 PM
Last Post: xSicKxBot
  [Tut] Python | Split Text into Sentences xSicKxBot 0 1,171 12-14-2022, 01:24 PM
Last Post: xSicKxBot
  [Tut] How to Split a Multi-line String into Multiple Lines? xSicKxBot 0 1,337 12-12-2022, 09:10 AM
Last Post: xSicKxBot
  [Tut] How to Convert an Octal Escape Sequence in Python – And Vice Versa? xSicKxBot 0 1,330 12-08-2022, 01:23 PM
Last Post: xSicKxBot

Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016