Skip to content
Advertisement

python: Convert a textfile into csv file

Say I want to convert a textfile (in.txt) into a csv file (out.csv) using Python.

in.txt:

ID: 1
Name: Jon Doe
Address: 123 Main St Anytown, USA
Age: 25

The output file out.csv should looks like this:

"ID","Name","Address","Age"
"1","Jon Doe","123 Main St Anytown, USA","25"

So far I have done this:

#!/usr/bin/python
#-*- coding:utf-8 -*-

import csv

f = open("in.txt")
x = f.readlines()
s = []

for i in x:
    i = i.replace(":",'"')
    j = i.replace(" ","'")
    
csvex = csv.writer(open("out.csv","w"), quoting=csv.QUOTE_ALL)
csvex.writerow(s)

How can I do this?

Advertisement

Answer

A solution that would allow either a single or multiple records:

def read_records(fn_in, fn_out, record_size):
    with open(fn_out, 'w') as out_f:
        write_header = True
        with open(fn_in) as in_f:
            while True:
                try:
                    rec = {
                        f'"{key.strip()}"': f'"{value.strip()}"' for key, value in
                        [next(in_f).split(':') for _ in range(record_size)]
                    }
                    if write_header:
                        write_header = False
                        out_f.write(','.join(rec.keys()) + 'n')
                    out_f.write(','.join(rec.values()) + 'n')
                except StopIteration:
                    break


read_records('in.txt', 'out.csv', 4)

Some explanation:

  • everything sits in a function, so you can use it repeatedly and for different files and record sizes
  • the while True loop loops forever, until a break statement breaks out of it
  • the try .. except StopIteration catches when next(in_f) tries to read beyond the end of the file
  • rec is created as a dictionary, which is handy in case you need to further manipulate the values or want to use the record elsewhere, although it’s not strictly speaking the most efficient way to perform the task.
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement