I have a function which reads and writes to a file but after each run and write cycle , i keep losing some part of the string and part of the float.
The function is given below with output which is saved in a file :
def stringlist(self,name,price):
b = price
hh = []
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=",", fmt ="%s")
n = np.append(m,g)
np.savetxt("D:\Python\inlint4.txt" , n, delimiter=', ', fmt="%s")
return n
And the output is saved two lines per cycle as follows :
RPOWE
54.5
RPOWE
57.34
RPOWE
57.75
RPOWER-EQ
57.9000015259
As you can see the only the last line is printed in full, all others are depreciated.
Also is there a way i can make this print like :
RPOWER-EQ , 57.9000015259
RPOWER-EQ , 57.9000015259
When I try to replicate your code
gives me an error
It looks like your
g
array (andm
) is a mix of strings and numbers. So it will be of dtype string or objectI suspect that the loss of float significant figures has to do with this conversion to/from strings. Maybe you could print
g
andm
to see their dtype and content.I can include your word in the 'fmt', thus:
However
loadtxt
does not like to load this; I'd have to specify a custom converter.genfromtxt
handles this okYou could skip the arrays entirely, by working with lists and file write directly, e.g.: