Skip to content Skip to sidebar Skip to footer

Parsing Text File With Json-like Object Into Csv

I have a text file containing key-value pairs, with the last two key-value pairs containing JSON-like objects that I would like to split out into columns and write with the other v

Solution 1:

I'd use:

from itertools import chain
import csv

_header_translate = {
    'StartPoint': ('start1', 'start2', 'start3'),
    'EndPoint': ('end1', 'end2', 'end3')
}

defheader(col):
    header = col.strip('{}').split('::', 1)[0]
    return _header_translate.get(header, (header,))

defcleancolumn(col):
    col = col.strip('{}').split('::', 1)[1]
    return col.split('[%2C]')

defchainedmap(func, row):
    returnlist(chain.from_iterable(map(func, row)))

withopen('input.txt', 'rb') as fin, open('output.csv', 'wb') as fout:
    reader = csv.reader(fin)
    writer = csv.writer(fout)
    for i, row inenumerate(reader):
        ifnot i:  # first row, write header first
            writer.writerow(chainedmap(header, row))
        writer.writerow(chainedmap(cleancolumn, row))

The cleancolumn method takes any of your columns and returns a tuple (possibly with only one value) after removing the braces, removing everything before the first :: and splitting on the embedded 'comma'. By using itertools.chain.from_iterable() we turn the series of tuples generated from the columns into one list again for the csv writer.

When handling the first line we generate one header row from the same columns, replacing the StartPoint and EndPoint headers with the 6 expanded headers.

Post a Comment for "Parsing Text File With Json-like Object Into Csv"