1

I have a little piece of code which is running well, however I am struggling to determine a way of outputting it into a .txt file. Here is the code:

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])):
            print line

The print command works and displays what I need in the terminal, however I just can't manage to find a way to save this yet. Here is what I have tried so far:

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

I am still quite inexperienced at python and code in general and was hoping somebody could explain the correct formatting to be used here. Thanks in advance.

Vlad
  • 135
  • 2
  • 13
  • You might want to check [documentation](http://docs.scipy.org/doc/numpy/reference/generated/numpy.savetxt.html) or [this question](http://stackoverflow.com/questions/16621351/how-to-use-python-numpy-savetxt-to-write-strings-and-float-number-to-an-ascii-fi). – Lafexlos Jun 19 '15 at 05:27
  • I already checked both before posting. The other thread is where I got my 4th listed attempt from and I was unable to solve it from the documentation. I just tried ...fmt="%f" but it didn't work either. – Vlad Jun 19 '15 at 05:40
  • Just to be sure - is the `line` numpy array object here `np.savetxt('inside.txt', line)` ? Can you show its data? – erthalion Jun 19 '15 at 05:53
  • @erthalion I can use the command "print line" after having performed the rest of my code and it displays exactly what I desire on the terminal screen - I just can't figure out how to save this data to a text file. – Vlad Jun 19 '15 at 06:07
  • 1
    @Vlad `AttributeError: 'str' object has no attribute 'str'` this means, that `line` is a string (and for sure, it's a line of file). But as you can see in documentation, `np.savetxt` requires numpy array as second argument, not just a string. – erthalion Jun 19 '15 at 06:11
  • @erthalion Forgive me if I am being obtuse here, but isn't that what I did in my final attempt? `np.savetxt('inside.txt', line, delimiter=" ", fmt="%s")` Is there something simple that I am missing? – Vlad Jun 19 '15 at 06:28
  • @Vlad no, `delimiter` and `fmt` options just describe an output file format ("inside.txt" in this case), not `line` format. – erthalion Jun 19 '15 at 06:40

1 Answers1

1

From documentation , you can clearly see that np.savetext requires an array_like object as the second argument.

You can try converting line into an array before saving , something like -

np.savetxt('inside.txt', np.array(line.split(" ")), delimiter=" ", fmt="%s")
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
  • 1
    As a small test you can try using `np.savetxt('inside.txt', line.split(" "), delimiter=" ", fmt="%s")` , we can check if it would accept list as input as well or not , my guess is no, but if by any luck if it does, we can avoid the overhead call to `np.array()` – Anand S Kumar Jun 19 '15 at 06:48
  • Thank you for your answer and explanation. – Vlad Jun 19 '15 at 09:03