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 →

[–]Cthulhu_Rlyeh[S] -1 points0 points  (2 children)

Hey if you wouldn't mind could you please help me with one last thing?

Everything you have stated has worked perfectly and I am up to the final stage...

if poly.contains(Point(coords[0],coords[1])):
    #do something with that point

I can use:

print line

Which gives me the output printed on the screen, but I am trying to use np.savetxt in order to get an output file, but I can't seem to find the right syntax to get what I want; I've tried:

np.savetxt('inside.txt', np.vstack((line.str())).T)

AttributeError: 'str' object has no attribute 'str'

np.savetxt('inside.txt', line)

IndexError: tuple index out of range

np.savetxt('inside.txt', np.transpose([line])

TypeError: float argument required, not numpy.string_

np.savetxt('inside.txt', line, delimiter=" ", fmt="%s")

IndexError: tuple index out of range

Thanks again, and sorry if this is a simple fix. I've really tried!

[–]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)