-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?

3
  • I just edited your post, plus I indented the print line as it wouldn't be correct otherwise
    – EdChum
    May 23, 2014 at 7:41
  • 1
    but it doesnt work. please be more specific. We cannot help if we don't know what's wrong
    – Tim
    May 23, 2014 at 7: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, 2014 at 7:43

1 Answer 1

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/

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.