我正在尝试将矩阵添加到现有的csv文件中。 在this链接之后,我编写了以下代码,
f_handle = file(outfile+'.x.betas','a')
np.savetxt(f_handle,dataPoint)
f_handle.close()
我将numpy导入为np,即
import numpy as np
但是我收到了这个错误:
f_handle = file(outfile +'。x.betas','a')
TypeError:'str'对象不可调用
我无法弄清楚问题似乎是什么。 请帮助:)
答案 0 :(得分:12)
看起来你可能已经定义了一个名为file
的变量,它是一个字符串。 Python然后抱怨str
对象遇到
file(...)
正如Bitwise所说,您可以通过将file
更改为open
来避免此问题。
您也可以通过不命名变量file
来避免此问题。
如今,打开文件的最佳方法是使用with
-statement:
with open(outfile+'.x.betas','a') as f_handle:
np.savetxt(f_handle,dataPoint)
这保证了当Python离开with
- 套件时文件被关闭。
答案 1 :(得分:2)
将file()
更改为open()
,应解决此问题。