【问题标题】:How do you convert a numpy.float64 to Python native string type for writing to a file with write()?如何将 numpy.float64 转换为 Python 本机字符串类型以使用 write() 写入文件?
【发布时间】:2018-11-25 16:48:51
【问题描述】:

如何将 numpy.float64 转换为 Python 原生字符串类型,以便使用 write() 写入文件?

【问题讨论】:

标签: python string numpy


【解决方案1】:

只需使用str():

import numpy as np

x = np.array([1, 2, 3, 4])

print(type(x))

print(type(str(x)))

产量:

<class 'numpy.ndarray'>
<class 'str'>

【讨论】:

    【解决方案2】:

    您也可以使用numpy直接将数组值写入文件:

    np.savetxt(filename,myarray)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-09
      • 2012-03-02
      • 2019-04-14
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 2012-09-19
      相关资源
      最近更新 更多