Trying to save an np array with string and floats, but getting a error

206 views Asked by At

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.

0

There are 0 answers