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()