MaxPower
MaxPower

Reputation: 425

Parsing a text file and grouping certain values

I am trying to read a text file and parse it in a specific way, then rewrite the file with the output.

The text file (input) looks like this:

2 108 1 561 1 20 28 1
2 108 2 557 1 24 32 1
5 28 1 553 197 20 20 1
5 28 2 552 197 23 21 1
6 23 1 113 393 36 36 1
6 23 2 113 391 39 39 1

Every column represents a specific value as so:

[ID] [Length] [Frame] [X] [Y] [W] [H]

So for an example, this line:

2 108 1 561 1 20 28 1

is actualy: ID:2, Length:108, Frame:1, X:561, Y:1, W:20, Y:28

The last value 1 is not needed at all.

Now here is how i'm doing it so far:

with open('1.txt') as fin:
    frame_rects = {}
    for row in (map(int, line.split()) for line in fin):
        id, frame, rect = row[0], row[2], row[3:7]
        frame_rects[frame] = (id, rect)
        first_data = ('{} {} {}\n'.format(frame, id, rect))
        print first_data

And this outputs the following:

1 2 [561, 1, 20, 28]

2 2 [557, 1, 24, 32]

1 5 [553, 197, 20, 20]

2 5 [552, 197, 23, 21]

1 6 [113, 393, 36, 36]

2 6 [113, 391, 39, 39]

this is the first step, but my expected output is as follows:

1 2 [561, 1, 20, 28] 5 [553, 197, 20, 20] 6 [113, 393, 36, 36]
2 2 [557, 1, 24, 32] 5 [552, 197, 23, 21] 6 [113, 391, 39, 39]

So for every frame, i append all of the ID's and their values that appear in that specific frame.

So in frame 1, id 2, 5 and 6 appeared each with its own values (x,y,w,h).

Every frame key is unique but can hold as many ID's + values as needed as long as they actually appeared in that frame.

I need to run this on text files that can potentially contain thousands of files. Every frame can hold ~20 different ID's. How will i be able to achieve that expected output?

Upvotes: 2

Views: 105

Answers (2)

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250971

from collections import defaultdict
with open('abc') as f:
    dic = defaultdict(list)
    for line in f:
        idx, lenght, frame, X, Y, W, H, _ = map(int, line.split())
        dic[frame].append([idx, [X, Y, W, H] ])
print dic
print "Expected output:"
for k, v in dic.items():
    print "{} {}".format(k, "".join(["{} {} ".format(*lis) for lis in v  ])  )

output:

defaultdict(<type 'list'>,
{1: [[2, [561, 1, 20, 28]], [5, [553, 197, 20, 20]], [6, [113, 393, 36, 36]]],
 2: [[2, [557, 1, 24, 32]], [5, [552, 197, 23, 21]], [6, [113, 391, 39, 39]]]})
Expected output:
1 2 [561, 1, 20, 28] 5 [553, 197, 20, 20] 6 [113, 393, 36, 36] 
2 2 [557, 1, 24, 32] 5 [552, 197, 23, 21] 6 [113, 391, 39, 39] 

Upvotes: 3

Inbar Rose
Inbar Rose

Reputation: 43447

Do this:

from collections import defaultdict

with open('1.txt') as fin:
    frame_rects = defaultdict(list)
    for row in (map(int, line.split()) for line in fin):
        id, frame, rect = row[0], row[2], row[3:7]
        frame_rects[frame].append((id, rect))
        # print '{} {} {}'.format(frame, id, rect) # (if you want to sample)

for key, value in frame_rects.items():
    print key, ' '.join([' '.join([str(i) for i in v]) for v in value])

Output:

1 2 [561, 1, 20, 28] 5 [553, 197, 20, 20] 6 [113, 393, 36, 36]
2 2 [557, 1, 24, 32] 5 [552, 197, 23, 21] 6 [113, 391, 39, 39]

Upvotes: 2

Related Questions