4

I have this input file

1 2
10 2
81 3
23 6
2537857295 19
34271891003654321 1267253

I am reading the file like this

with open("powersearch.txt") as fileIn:
    for line in fileIn:
        print line

I am wondering if I want to, for every single line, have the 1st integer stored as firstNum, the 2nd stored as secondNum. With Java I can use a scanner and do nextInt() and hasNext() to get the integers, what are the equivalent in Python?

4 Answers 4

6

Well to parse an int from a string you just use int(s), where s is the string.

I think this would be the most logic way in your example:

with open("powersearch.txt") as fileIn:
    for line in fileIn:
        n1, n2 = (int(s) for s in line.split())
        print(n1, n2)

Python is a different language than Java, and in my opinion more expressive (I can do more in one line than I can in Java and still write readable code). If you try to write Java stuff in Python you'll find the language a lot less effective than it can be.

6

Please discard the Java ideas. They will just confuse you.

Python is a different language, and you must learn the Pythonic approach.

Here's an example.

with open("powersearch.txt") as fileIn:
    for firstNum, secondNum in ( map(int, line.split()) for line in fileIn ):
        print firstNum, secondNum
2
  • There's a comma missing somewhere in there Sep 12, 2011 at 23:12
  • 1
    I think "map(int(line.split())" should be "map(int,line.split())".
    – Weeble
    Sep 12, 2011 at 23:12
3

EDIT - nightcracker's solution is pretty much equivalent now.

(I'm assuming you're using Python 2.x and not 3.x from the syntax.)

It's a matter of taste, but I would prefer a mix of S.Lott and nightcracker's solutions:

with open("powersearch.txt") as fileIn:
    for line in fileIn:
        firstNum, secondNum = [int(s) for s in line.split()]
        print firstNum, secondNum

List comprehension is a bit easier to read than map for me. Destructuring assignment lets us take the two item list and assign the element values to two different local variables at once.

2
  • 2
    Better use lazy evaluation and use (int(s) for s in line.split()) instead. If there are 100000 integers on a line the performance increase will be dramatic.
    – orlp
    Sep 12, 2011 at 23:30
  • If there are not exactly two integers on the line it will result in an error anyway. If you're worried about handling such a scenario more gracefully, a generator expression is only the start.
    – Weeble
    Sep 13, 2011 at 11:35
0

The answers before (n1, n2 = (int(s) for s in line.split())) will only work if you are using a couple of numbers. Otherwise, you will need to use the the map function.

with open("powersearch.txt") as fileIn:
    for line in fileIn:
        line_int = list(map(int, line.split()))
        print(line_int)

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.