-2

I had a file with some values like this:

(1, 3)
(4, 5)
(15, 39)

And use it like values (x, y).

I first would like to store the integer 1 into variable 'x' and then integer 3 into variable 'y', and then I would like to use them in a method. I then would like to store the second line, 4 into variable 'x' and 5 into variable 'y'. So basically reuse just two, 'x' and 'y', to use them temporarily in a method and store the next line of input into those variables, and so on. Example:

x = 1
y = 3
z = x + y
print z = 4
return x, y  #Here return to line 2 (4, 5)

print z = 9

and so on ....and stop when my file is done.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 3
    It sounds like you should work through a [basic Python tutorial](https://developers.google.com/edu/python/). – skrrgwasme Feb 13 '15 at 21:34
  • possible duplicate of [How to read numbers from file in Python?](http://stackoverflow.com/questions/6583573/how-to-read-numbers-from-file-in-python) – aruisdante Feb 13 '15 at 21:37

1 Answers1

0

Here you go:

#!/usr/bin/env python2.7

def read_file(filename):
    # opens file, reads contents, and splits it by newline
    with open(filename, 'r') as f:
        data = f.read().split('\n')

    # iterates through data and converts each string to a tuple
    for i in range(len(data)):
        lparen = data[i].find('(')
        comma = data[i].find(',')
        rparen = data[i].find(')')
        data[i] = (int(data[i][lparen+1:comma]), int(data[i][comma+1:rparen]))

    return data

def main():
    for i in read_file('ParsingCoordsInput.txt'):
        print i[0] + i[1]

if __name__ == '__main__':
    main()

A couple of notes:

  • I used Python 2.7 (if you used 3.x, then this code will need to be modified to run properly)
  • I named the input file ParsingCoordsInput.txt. If your file's name is different (as I assume it will be), you'll have to change the first line of main()
  • Make sure the input file is in the same directory (folder) as the script

EDIT:

Here's the code for Python 3.x. The only difference was adding parentheses around print (as it became a function in 3.0). Enjoy!

#!/usr/bin/env python3

def read_file(filename):
    # opens file, reads contents, and splits it by newline
    with open(filename, 'r') as f:
        data = f.read().split('\n')

    # iterates through data and converts each string to a tuple
    for i in range(len(data)):
        lparen = data[i].find('(')
        comma = data[i].find(',')
        rparen = data[i].find(')')
        data[i] = (int(data[i][lparen+1:comma]), int(data[i][comma+1:rparen]))

    return data

def main():
    for i in read_file('ParsingCoordsInput.txt'):
        print(i[0] + i[1])

if __name__ == '__main__':
    main()
pzp
  • 6,249
  • 1
  • 26
  • 38
  • How can i change this code to python 3? I've tried define read_file as global variable, and I have this error: ValueError: invalid literal for int() with base 10: ''. Can you help me give me some spoor? please – Théré Hernandez Feb 17 '15 at 19:44
  • @ThéréHernandez Your welcome. Being that I answered your question, I'd appreciate it if you marked my answer as correct (an up vote wouldn't hurt either :P). – pzp Feb 18 '15 at 16:16