0

I am trying to write a function to add an item, which if not present in a list with its name and price in a tuple , save the new entries to a file(txt) , which might have some entries in it already.

for this i wrote :

  def stringlist(self,name,price):
    b = price
    if name in self.x :
        c = self.x.index(name)
        d = (self.x[c], b)
        self.z.append(d)
    else:
        self.x.append(name)
        c = self.x.index(name)
        d = (self.x[c], b)
        self.z.append(d)
    g = np.asarray(self.z)
    m = np.loadtxt("D:\Python\inlint4.txt",  delimiter="," , dtype="|S5")
    n = np.append(m,g)
    n.astype(dtype="|S5")
    np.savetxt("D:\Python\inlint4.txt" , n, delimiter=', ', newline='\n')
    return n 

but i get an error on the line with np.savetxt (2nd last line) :

File "D:\Python\toffee\sample.py", line 62, in **stringlist**

np.savetxt("D:\Python\inlint4.txt" , n, delimiter=', ', newline='\n')

File "C:\Python27\lib\site-packages\numpy\lib\npyio.py", line 1087, in savetxt

fh.write(asbytes(format % tuple(row) + newline))

TypeError: float argument required, not numpy.unicode_

any idea or help, thanks.

Manipal King
  • 422
  • 1
  • 5
  • 18
  • what is your `hh` doing? You append `h` to it, but then try and write `n` to the file, and don't actually use `hh` – tmdavison Jun 08 '15 at 15:27
  • i if the save part would work , i have an idea for its use . but for now just avoid it @tom – Manipal King Jun 08 '15 at 15:29
  • ok. You might want to edit your code to be a [MCVE](http://www.stackoverflow.com/help/mcve) then (i.e. remove the unnecessary code). That would help those trying to answer your question – tmdavison Jun 08 '15 at 15:33
  • Looking at that duplicate, I think you need to set the `fmt` in your `savetxt` command – tmdavison Jun 08 '15 at 15:43

0 Answers0