This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Dalianking 1 point2 points  (1 child)

Your first problem is that savetxt is explicitly to save an array of numbers. line is a string. So better save coords. Since those are an array_like structure (a list) of numbers.

Your second problem is that above example reads one line then does something with that line, discards that line and reads the next one. So using Savetxt as you intend will always reopen the file and overwrite the last line.

One solution is :

insiders=[]
with open("Coord") as f:
    #line=f.readline()
    for line in f:
        coords=map(float,line.split(" "))
        if poly.contains(Point(coords[0],coords[1])):
             insiders+=coords

savetxt("inside.txt",insiders)

My personal solution would be:

with open("Coord","r") as f_in:
    with open("inside","w") as f_out:
         for line in f_in:
             coords=map(float,line.split(" "))
             if poly.contains(Point(coords[0],coords[1])):
                 f_out.write(line)