-3

i have a very large file, which can not be opened by kind of texteditor or something. And i need to Check if (1) the line starts with a specific string and (2) if a number at a specific position (col 148 (3 digits)) is smaller than a predefined number. This complete line should be printed then

so i tried the following code. but it doesnt work.

fobj = open("test2.txt")
for line in fobj:
    if (line.startswith("ABS")) and (fp.seek(3, 148) < 400): 
        print line.rstrip()

Can anyone help me?

EdChum
  • 376,765
  • 198
  • 813
  • 562
user3667966
  • 11
  • 1
  • 2
  • I just edited your post, plus I indented the `print line` as it wouldn't be correct otherwise – EdChum May 23 '14 at 07:41
  • 1
    *but it doesnt work.* please be more specific. We cannot help if we don't know what's wrong – Tim May 23 '14 at 07:42
  • 1
    I think you misunderstand the usage of `fp.seek` it doesn't return a value, plus even if it did you are comparing either a string or raw bytes to an int which is not going to work – EdChum May 23 '14 at 07:43

1 Answers1

2

To compare a number with a string you need to convert it:

int(fp.seek(3, 148)) < 400

You have to check the string to contain only numbers.

But seek() is not the function you are looking for, you can use it to skip the bytes of a file to a specific point.

Look here: seek() function?

If your number is always on the same position you can use:

int(line[148:150]) < 400

Try it with regular expressions and string operations:

http://pymotw.com/2/re/

Community
  • 1
  • 1
w5e
  • 199
  • 1
  • 12