Python - read numbers from text file and put into list

Posted by user1647372 on Stack Overflow See other posts from Stack Overflow or by user1647372
Published on 2012-09-04T21:19:15Z Indexed on 2012/09/04 21:38 UTC
Read the original article Hit count: 141

Filed under:

So like the title says im starting to learn some python and im having trouble picking up on this technique. What I need to accomplish is to read in some numbers and store them in a list. The text file looks like the following:

0 0 3 50

50 100 4 20

Basically these are coordinates and directions to be used for python's turtle to make shapes. I got that part down the only problem is getting them in a correct format. So what I can not figure out is how to get those numbers from the file into [ [0, 0, 3, 50], [50, 100, 4, 20] ] A list, with each four coordinates being a list in that one big list.

Heres my attempt but it as I said I need some help - thank you.

polyShape=[]
infile = open(name,"r")
num = int(infile.readline(2))
while num != "":
    polyShape.append(num)
    num = int(infile.readline(2))
infile.close()

© Stack Overflow or respective owner

Related posts about python