Losing values after reading from file and saving np array to the same file

2.2k views Asked by At

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
1

There are 1 answers

0
hpaulj On BEST ANSWER

When I try to replicate your code

m = np.loadtxt("D:\Python\inlint4.txt",  delimiter=",", fmt ="%s")

gives me an error

TypeError: loadtxt() got an unexpected keyword argument 'fmt'

It looks like your g array (and m) is a mix of strings and numbers. So it will be of dtype string or object

In [410]: z=np.asarray(['RPOWE',54.50124])

In [411]: z
Out[411]: 
array(['RPOWE', '54.50124'], 
      dtype='|S8')

In [412]: z=np.asarray(['RPOWE',54.50124],dtype=object)

In [413]: z
Out[413]: array(['RPOWE', 54.50124], dtype=object)

I suspect that the loss of float significant figures has to do with this conversion to/from strings. Maybe you could print g and m to see their dtype and content.


I can include your word in the 'fmt', thus:

In [419]: np.savetxt('temp',np.array([1.23,1324.243]),delimiter=',',fmt="%s, %%s"%'RPOWE')

In [420]: cat temp
RPOWE, 1.23
RPOWE, 1324.243

However loadtxt does not like to load this; I'd have to specify a custom converter.

genfromtxt handles this ok

In [423]: np.genfromtxt('temp',delimiter=',',dtype=None)
Out[423]: 
array([('RPOWE', 1.23), ('RPOWE', 1324.243)], 
      dtype=[('f0', 'S5'), ('f1', '<f8')])

You could skip the arrays entirely, by working with lists and file write directly, e.g.:

In [438]: alist=[('ROWAN',123.35234), ('RWRSR',343.23424), ('Testin',1.234)]

In [439]: for pair in alist:
    with open('temp','a') as f:
        f.write('%s, %f\n'%pair)
   .....:         

In [440]: cat temp
ROWAN, 123.352340
RWRSR, 343.234240
Testin, 1.234000

In [442]: np.genfromtxt('temp',dtype=None,delimiter=',')
Out[442]: 
array([('ROWAN', 123.35234), ('RWRSR', 343.23424), ('Testin', 1.234)], 
      dtype=[('f0', 'S6'), ('f1', '<f8')])