I have a file in the format below:
995957,16833579 995959,16777241 995960,16829368 995961,50431654
I want to read in each line but split the values into the appropriate values. For example the first line would be split into:
x = 995957 y = 16833579
Since its a string when you read it in and I want to convert them to an int and split them, how exactly would I go about doing this? Any help would be appreciated.
Something like this - for each line read into string variable
>>> a = "123,456" >>> b = a.split(",") >>> b ['123', '456'] >>> c = [int(e) for e in b] >>> c [123, 456] >>> x, y = c >>> x 123 >>> y 456
Now you can do what is necessary with
y as assigned, which are integers.
I would do something like:
filename = "mynumbers.txt" mynumbers =  with open(filename) as f: for line in f: mynumbers.append([int(n) for n in line.strip().split(',')]) for pair in mynumbers: try: x,y = pair,pair # Do Something with x and y except IndexError: print "A line in the file doesn't have enough entries."
The with open is recommended in http://docs.python.org/tutorial/inputoutput.html since it makes sure files are closed correctly even if an exception is raised during the processing.