You can use the csv module and the separator reader to read your data and use a script from the same module (with a comma delimiter) to create the output.
In fact, the first example in the csv module documentation uses delimiter=' ' .
You can use DictReader / DictWriter and specify the column order in its constructor ( fieldnames list: different for reading / writing, if you want to reorder) to display the records in the order you want.
(When creating output, you may need to skip / ignore your first two lines.)
EDIT:
Here is an example of using verbose country names:
import cStringIO import csv f = cStringIO.StringIO("""ABC 1 2 Costa Rica 3 4 Democratic Republic of the Congo """) r = csv.DictReader(f, delimiter=' ', restkey='rest') for row in r: if row.get('rest'): row['C'] += " %s" % (" ".join(row['rest'])) print 'A: %s, B: %s, C: %s' % (row['A'], row['B'], row['C'])
Use restkey= and combine the dict entry for this value, which is a list of the remaining ones (here restkey='rest' ). It means:
A: 1, B: 2, C: Costa Rica A: 3, B: 4, C: Democratic Republic of the Congo
source share