I have a Python script written where I'm trying to filter some text files and compare them to another text file, however I'm struggling to find a solution.
below | 1
above | 2
above | 3
above | 4
above | 5
below | 6
below | 7
below | 8
below | 9
below | 10
below | 11
below | 12
above | 13
below | 14
below | 15
below | 16
below | 17
below | 18
below | 19
below | 20
below | 21
...
I have this file listing video frames and whether they're above or below a defined threshold.
Additionally, I have a list of those 'above' threshold frames, and a user-defined value tagged (either x, y or z) to each. Unfortunately the numbers in this list do not correspond to the initial above-or-below frame number, but instead are just a numbered list.
y | 1
x | 2
x | 3
y | 4
z | 5
z | 6
y | 7
z | 8
y | 9
y | 10
x | 11
x | 12
y | 13
x | 14
x | 15
x | 16
x | 17
x | 18
y | 19
x | 20
z | 21
I want to combine these two such that the x, y or z values of the above frames replace the 'above' tag in the other script, like this:
below | 1
y | 2
x | 3
x | 4
y | 5
below | 6
below | 7
below | 8
below | 9
below | 10
below | 11
below | 12
z | 13
below | 14
below | 15
below | 16
below | 17
below | 18
below | 19
below | 20
below | 21
However I can't get my head round how to iterate through the list to achieve this. Should I be storing values in a dictionary and iterating over those? Any help would be much appreciated. I've tried using some for loops and with open statements to try it but I can't get my head round how to iterate through it:
with open((selectedvideostring + 'combinedfiles.txt'), 'w') as combinedfile:
with open((selectedvideostring + 'aboveorbelow.txt'), 'r') as aboveorbelowfile:
for line in aboveorbelowfile:
if 'above' in line:
with open((selectedvideostring + 'xyzfile.txt'), 'r') as xyzfile:
for line in xyzfile:
if 'x' in line:
combinedfile.write("x" + '|' + str(int(cap.get(1))))
elif 'y' in line:
combinedfile.write("y" + '|' + str(int(cap.get(1))))
if 'z' in line:
combinedfile.write("z" + '|' + str(int(cap.get(1))))
elif 'below' in line:
combinedfile.write("below" + '|' + str(int(cap.get(1))))
Thanks!