【发布时间】:2016-05-26 17:21:36
【问题描述】:
我最近切换到 Python 3。在我的代码中,我有一个 numpy save as text 命令
f_handle = open('results.log','a')
f_handle.write('Some text')
numpy.savetxt(f_handle, X, delimiter=',', fmt='%.4f')
在 Python 3 中,这会导致 numpy 命令出错,标志需要为 'ab',即以二进制形式写入。现在我将几个 write 语句一个接一个地混合,所以为了调用 Numpy 命令,我必须做这样的事情,
f_handle = open('results.log','a')
f_handle.write('Some text...')
f_handle.close()
f_handle = open('results.log','ab')
numpy.savetxt(f_handle, X, delimiter=',', fmt='%.4f')
f_handle.close()
f_handle = open('results.log','a')
f_handle.write('Some more text...')
这似乎是一种非常无效的做事方式,尤其是在您编写很多东西的情况下。那我该怎么做呢?
【问题讨论】:
标签: python python-3.x numpy io