Python Forum

Full Version: Adding Sliced Positions
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, I'm reading a large file line by line and I need to know if certain positions are greater than 0 if not, I don't want to print that line, it should then go to the next line. Example of line below.

Output:
000002250000000000089877000000000000.0002/28/202000000000.00000004698.45
for line in mf.readlines():
if(line[164:171] > 0)

....then print
 else next line
So if 4698.45 > 0

Would each position need to be assigned to a variable and then summed or is there a better way? If someone could point me in the right direction. Thanks!
Okay so far. But when reading from a file, the positions are strings, so you'll have to attempt to cast it to a number to compare, or you'll have to ask if it has any digits other that zero.

for line in mf.readlines():
    if int(line[164:171]) > 0
        print(line)