1

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!

4
  • 1
    what did you try already? Commented Apr 24, 2017 at 10:34
  • Please paste the content of those files into your question so we don't have to re-type all that stuff in order to test a possible solution. Commented Apr 24, 2017 at 10:36
  • Will do that now, apologies! Commented Apr 24, 2017 at 10:37
  • Right, pasted the content, and the rubbish code I have so far! Commented Apr 24, 2017 at 10:54

1 Answer 1

1

Instead of opening and iterating the xyz file inside of the outer above-or-below file loop, you should open both files at once. Files are iterators, so you can use a for loop to iterate the lines in the above-or-below file, and use next to just get the next line from the xyz file whenever you encounter a "above" entry.

with open("aboveorbelow.txt") as aob, open("xyzfile.txt") as xyz:
    for line in aob:
        if line.startswith("above"):
            ab, c = line.split(" | ")
            d, _ = next(xyz).split(" | ")
            print(" | ".join((d, c)))
        elif line.startswith("below"):
            print(line)

(Using print for simplicity of testing, but of course the same works with an output file.)

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the help! One small issue, implementing that code in my program I'm getting an error, reading 'StopIteration', throwing the error on the line d, _ = next(xyz).split(" | ")
@KittenMittons In this case, there are not enough xyz values in your second file. You could use next with a default value, if a useable default exists, e.g. next(xyz, "UNKNOWN | 0").split(" | ").
Thanks very much - that has stopped the StopIteration issue - when I use those print statements as above it outputs exactly as I want it, however when I try to write those lines to a new output file, it completes with no errors, yet no file exists - I'll add the code I'm using as implemented as an edit in the original question now, I must be doing something wrong
Never mind, it was me not saving the correct file path - I've sorted it, thanks so much for all your help!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.