George George - 1 year ago 107
Python Question

python read only integers from file

I can't figure the way to read from this file, only the integers:

2 48

The function should return:

[34, -1, 0, 1, -1]

If a number has
is valid. But if it has
or any letters is not.

If it has a space (as for example
2 48
) is not valid.

If it is > 999 it is not valid.

I am stuck only at here:

my_list = []
with open('test.txt') as f:
lines = f.readlines()
for line in lines:

I tried to make it a string and use punctuation using
but I am not sure if it gets more complicated.

Also, I am not sure about using regex. I tried a simple regex but I don't have experience using it.

Answer Source

If you wanted to do this via regular expressions:

import re
exp = re.compile(r'^[\+,\-]?[0-9]{1,3}$')

my_list = []
with open('input.txt') as f:
    lines = f.readlines()
    for line in lines:
        if re.match(exp, line.strip()):

Lets explain the regular expressions.

^[\+,\-]? - ^ means the expression must start with the next qualifiers, which are a list of two characters \+ and \-. We need the escaping slashes there to actually put in the special chars in. The final ? makes the preceding argument optional (so the string can start with a + or -, or nothing).

[0-9]{1,3}$ - [0-9] specifies the set of characters that are numbers. {1,3} specifies that they should occur a minimum of one time, or a max of 3 times (hence satisfying your <999 constraint. The $ sign matches the end of the string, so the string must end with this set of chars.

Hope this all helps.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download