Adding a matrix to an existing file using numpy

I am trying to add a matrix to an existing csv file. Following this link, I wrote the following code,

f_handle = file(outfile+'.x.betas','a') np.savetxt(f_handle,dataPoint) f_handle.close() 

where I imported numpy as np, i.e.

 import numpy as np 

But I get this error:

f_handle = file (outfile + '. x.betas', 'a')
TypeError: object 'str' cannot be called

I can’t understand what the problem is. Please, help:)

+4
source share
2 answers

It looks like you could define a variable called file , which is a string. Python then complains that str objects are not callable when it encounters

 file(...) 

You can avoid the problem by changing it by changing file to open .

You can also avoid the problem by not naming the file variable.

Currently, the best way to open a file is with -statement :

 with open(outfile+'.x.betas','a') as f_handle: np.savetxt(f_handle,dataPoint) 

This ensures that the file is closed when Python leaves with -suite.

+11
source

Change file() to open() , which should solve it.

+2
source

Source: https://habr.com/ru/post/1492154/


All Articles