Amanda
Amanda

Reputation: 12737

I've got a funky tab delimited file that I'd like to make less funky

I have a lot of tab delimited data that is organized into multiple rows and should be all in two rows. Now it looks like this:

    Some Key    Other key   Foo Key    Bar Key
     0           5           18         12

    More Key    Dor key     Gee Key    Francis Scott  Key
     19          14          8          0

    Wa Key      Whis key    Don Key    Luh Key
     0           2           8          16

And I need it to look like this:

    Some Key    Other key   Foo Key    Bar Key    More Key    Dor key     Gee Key    Francis Scott  Key    Wa Key      Whis key    Don Key    Luh Key
     0           5           18         12         19          14          8          0                    0           2           8          16

I've got a few hundred csv files with about 20 rows each, 30 columns, so I want to script my way through this as much as possible. I'm using Python CSV, but I can't figure out how to explain myself to it.

Upvotes: 3

Views: 149

Answers (2)

Michael Hoffman
Michael Hoffman

Reputation: 34314

This should do it:

import csv
import sys

keys = []
values = []

reader = csv.reader(sys.stdin, delimiter="\t")
for row in reader:
    keys.extend(row)
    values.extend(next(reader))

    # skip empty line
    assert next(reader, []) == []

writer = csv.writer(sys.stdout, delimiter="\t")
writer.writerow(keys)
writer.writerow(values)

Save this as thingie.py and run it with python thingie.py < sample.csv (where "sample.csv" is your data file)

Upvotes: 3

bigblind
bigblind

Reputation: 12867

You could ssplit the contents of your file with the tab as a separator, taking every int"eger element and putting it in one array, and taking every string element and putting it in another one. Then, you put out all the strings (keys) on one line separated by tabs, and one line containing all the integers separated by tabs.

Upvotes: 0

Related Questions