Python splitting group of lines in text file in separate files based on pattern
Question:
I have text file structured as following:
{0: (566, 906), 1: (434, 750), 2: (1021, 649), 3: (857, 567), 4: (761, 654), 5: (929, 534), 6: (1418, 465), 7: (1345, 985), 8: (753, 465), 9: (134, 522), 10: (614, 473), 11: (1868, 591), 12: (881, 473), 13: (1112, 434), 14: (688, 451), 15: (843, 423)} {0: (563, 920), 1: (429, 755), 2: (1023, 652), 3: (859, 569), 4: (761, 659), 5: (931, 536), 6: (1410, 465), 7: (1363, 988), 8: (753, 466), 9: (134, 522), 10: (613, 475), 11: (1858, 584), 12: (881, 474), 13: (1109, 434), 14: (688, 452), 16: (1702, 617), 17: (945, 463), 18: (820, 400)} {0: (558, 934), 1: (424, 763), 2: (1025, 653), 3: (861, 572), 4: (762, 663), 5: (931, 537), 6: (1404, 463), 7: (1379, 993), 8: (753, 466), 9: (134, 522), 10: (613, 476), 11: (1849, 578), 12: (881, 475), 13: (1101, 431), 14: (688, 453), 17: (947, 464), 19: (1677, 610)}
Every number before :
represents an object number and numbers between braces represent x,y
coordinates the whole line between {}
represents all objects "vehicles" in the video frame.
Can I group all objects "vehicles" and their numbers for every frame in separate files using python?
The output should be something like this:
file1.text
object number 0 and it’s coordinates in all frames
0: (566, 906) 0: (563, 920) 0: (558, 934)
file2.text
object number 1 and it’s coordinates in all frames
1: (434, 750) 1: (429, 755) 1: (424, 763)
and so on for all objects in all data frame
Answers:
This will do it. Straightforward; just read the data into a list of dictionaries, then reformat it into a dictionary of lists.
import ast
from collections import defaultdict
from pprint import pprint
data = [ast.literal_eval(ln) for ln in open('x.json')]
print(data)
result = defaultdict(list)
for row in data:
for k,v in row.items():
result[k].append(v)
pprint(result)
Output:
[{0: (566, 906), 1: (434, 750), 2: (1021, 649), 3: (857, 567), 4: (761, 654), 5: (929, 534), 6: (1418, 465), 7: (1345, 985), 8: (753, 465), 9: (134, 522), 10: (614, 473), 11: (1868, 591), 12: (881, 473), 13: (1112, 434), 14: (688, 451), 15: (843, 423)}, {0: (563, 920), 1: (429, 755), 2: (1023, 652), 3: (859, 569), 4: (761, 659), 5: (931, 536), 6: (1410, 465), 7: (1363, 988), 8: (753, 466), 9: (134, 522), 10: (613, 475), 11: (1858, 584), 12: (881, 474), 13: (1109, 434), 14: (688, 452), 16: (1702, 617), 17: (945, 463), 18: (820, 400)}, {0: (558, 934), 1: (424, 763), 2: (1025, 653), 3: (861, 572), 4: (762, 663), 5: (931, 537), 6: (1404, 463), 7: (1379, 993), 8: (753, 466), 9: (134, 522), 10: (613, 476), 11: (1849, 578), 12: (881, 475), 13: (1101, 431), 14: (688, 453), 17: (947, 464), 19: (1677, 610)}]
defaultdict(<class 'list'>,
{0: [(566, 906), (563, 920), (558, 934)],
1: [(434, 750), (429, 755), (424, 763)],
2: [(1021, 649), (1023, 652), (1025, 653)],
3: [(857, 567), (859, 569), (861, 572)],
4: [(761, 654), (761, 659), (762, 663)],
5: [(929, 534), (931, 536), (931, 537)],
6: [(1418, 465), (1410, 465), (1404, 463)],
7: [(1345, 985), (1363, 988), (1379, 993)],
8: [(753, 465), (753, 466), (753, 466)],
9: [(134, 522), (134, 522), (134, 522)],
10: [(614, 473), (613, 475), (613, 476)],
11: [(1868, 591), (1858, 584), (1849, 578)],
12: [(881, 473), (881, 474), (881, 475)],
13: [(1112, 434), (1109, 434), (1101, 431)],
14: [(688, 451), (688, 452), (688, 453)],
15: [(843, 423)],
16: [(1702, 617)],
17: [(945, 463), (947, 464)],
18: [(820, 400)],
19: [(1677, 610)]})
I have text file structured as following:
{0: (566, 906), 1: (434, 750), 2: (1021, 649), 3: (857, 567), 4: (761, 654), 5: (929, 534), 6: (1418, 465), 7: (1345, 985), 8: (753, 465), 9: (134, 522), 10: (614, 473), 11: (1868, 591), 12: (881, 473), 13: (1112, 434), 14: (688, 451), 15: (843, 423)} {0: (563, 920), 1: (429, 755), 2: (1023, 652), 3: (859, 569), 4: (761, 659), 5: (931, 536), 6: (1410, 465), 7: (1363, 988), 8: (753, 466), 9: (134, 522), 10: (613, 475), 11: (1858, 584), 12: (881, 474), 13: (1109, 434), 14: (688, 452), 16: (1702, 617), 17: (945, 463), 18: (820, 400)} {0: (558, 934), 1: (424, 763), 2: (1025, 653), 3: (861, 572), 4: (762, 663), 5: (931, 537), 6: (1404, 463), 7: (1379, 993), 8: (753, 466), 9: (134, 522), 10: (613, 476), 11: (1849, 578), 12: (881, 475), 13: (1101, 431), 14: (688, 453), 17: (947, 464), 19: (1677, 610)}
Every number before :
represents an object number and numbers between braces represent x,y
coordinates the whole line between {}
represents all objects "vehicles" in the video frame.
Can I group all objects "vehicles" and their numbers for every frame in separate files using python?
The output should be something like this:
file1.text
object number 0 and it’s coordinates in all frames
0: (566, 906) 0: (563, 920) 0: (558, 934)
file2.text
object number 1 and it’s coordinates in all frames
1: (434, 750) 1: (429, 755) 1: (424, 763)
and so on for all objects in all data frame
This will do it. Straightforward; just read the data into a list of dictionaries, then reformat it into a dictionary of lists.
import ast
from collections import defaultdict
from pprint import pprint
data = [ast.literal_eval(ln) for ln in open('x.json')]
print(data)
result = defaultdict(list)
for row in data:
for k,v in row.items():
result[k].append(v)
pprint(result)
Output:
[{0: (566, 906), 1: (434, 750), 2: (1021, 649), 3: (857, 567), 4: (761, 654), 5: (929, 534), 6: (1418, 465), 7: (1345, 985), 8: (753, 465), 9: (134, 522), 10: (614, 473), 11: (1868, 591), 12: (881, 473), 13: (1112, 434), 14: (688, 451), 15: (843, 423)}, {0: (563, 920), 1: (429, 755), 2: (1023, 652), 3: (859, 569), 4: (761, 659), 5: (931, 536), 6: (1410, 465), 7: (1363, 988), 8: (753, 466), 9: (134, 522), 10: (613, 475), 11: (1858, 584), 12: (881, 474), 13: (1109, 434), 14: (688, 452), 16: (1702, 617), 17: (945, 463), 18: (820, 400)}, {0: (558, 934), 1: (424, 763), 2: (1025, 653), 3: (861, 572), 4: (762, 663), 5: (931, 537), 6: (1404, 463), 7: (1379, 993), 8: (753, 466), 9: (134, 522), 10: (613, 476), 11: (1849, 578), 12: (881, 475), 13: (1101, 431), 14: (688, 453), 17: (947, 464), 19: (1677, 610)}]
defaultdict(<class 'list'>,
{0: [(566, 906), (563, 920), (558, 934)],
1: [(434, 750), (429, 755), (424, 763)],
2: [(1021, 649), (1023, 652), (1025, 653)],
3: [(857, 567), (859, 569), (861, 572)],
4: [(761, 654), (761, 659), (762, 663)],
5: [(929, 534), (931, 536), (931, 537)],
6: [(1418, 465), (1410, 465), (1404, 463)],
7: [(1345, 985), (1363, 988), (1379, 993)],
8: [(753, 465), (753, 466), (753, 466)],
9: [(134, 522), (134, 522), (134, 522)],
10: [(614, 473), (613, 475), (613, 476)],
11: [(1868, 591), (1858, 584), (1849, 578)],
12: [(881, 473), (881, 474), (881, 475)],
13: [(1112, 434), (1109, 434), (1101, 431)],
14: [(688, 451), (688, 452), (688, 453)],
15: [(843, 423)],
16: [(1702, 617)],
17: [(945, 463), (947, 464)],
18: [(820, 400)],
19: [(1677, 610)]})