Q:

Comparing consecutive Numbers in .txt file in Python

I am using Python 2.7 and I am struggling with a problem in my script:
I want to read a .txt file and compare numbers in consecutive rows, and find at least 5 consecutive ones, which are below a threshold and save their position (the numbers of the lines) into another .txt file and stop when the input file is completed. 
Could anyone think of a code to implement this? I've tried with fobj and numbers but it won't work.
My input file looks something like this:
0.1123
0.1233
0.5343
3.7654
-3.4325
-2.9832
10.3289
11.3890
...

And my desired output would be the lines (in this case lines 0 to 5 if we assume the threshold to be 10.0):
0
1
2
3
4
5
...

Thanks in advance!

A:

If I understand correctly this should be what you are looking for. You would need to tweak the code a bit if you only wanted a certain number of entries.
threshold = 10
old_file = 'old_file.txt'
with open(old_file, 'r') as document:
    values = [x for x, value in enumerate(document) if float(value) < threshold]

new_file = open('new_file.txt', 'w')
for x in values:
    new_file.write(str(x) + '\n')

