Skip to content Skip to sidebar Skip to footer

Use Python To Manipulate Txt File Presentation Of Key-value Grouping

I am trying to use Python in order to manipulate a text file from Format A: Key1 Key1value1 Key1value2 Key1value3 Key2 Key2value1 Key2value2 Key2value3 Key3... I

Solution 1:

Do it in one pass, without storing the lines:

withopen("input") as infile, open("ouptut", "w") as outfile:
    for line in infile:
        if line.startswith("chr"):
            key = line.strip()
        else:
            print >> outfile, key, line.rstrip("\n")

This code assumes the first line contains a key and will fail otherwise.

Solution 2:

First, if strings start with a character sequence, don't use regular expressions. Much simpler and easier to read:

if line.startswith("chr")

The next step would be to use a very simple state machine. Like so:

current_key = ""for line in file:
    if line.startswith("chr"):
        current_key = line.strip()

    else:
        print" ".join([current_key, line.strip()])

Solution 3:

If there are always the same number of values per key, islice is useful:

from itertools import islice

withopen('input.txt') as fin, open('output.txt','w') as fout:
    for k in fin:
        for v in islice(fin,3):
            fout.write(' '.join((k.strip(),v)))

Post a Comment for "Use Python To Manipulate Txt File Presentation Of Key-value Grouping"