【问题标题】:data type error while saving file to csv in python在python中将文件保存到csv时出现数据类型错误
【发布时间】:2016-09-23 23:26:11
【问题描述】:

我只是想使用 numpy 将数据保存到 python 中的 csv 文件。

这就是我正在做的:

np.savetxt('data.csv', array, delimiter=',', fmt='%.4f')

但是我收到以下错误

Mismatch between array dtype ('<U1') and format specifier ('%.4f')

这个 dtype 是什么,它是什么意思? 任何帮助将不胜感激

【问题讨论】:

  • 告诉我们您的阵列。重要的东西,比如它的 dtype 和 shape。

标签: python csv numpy


【解决方案1】:

np.array 的dtype 是它的数据类型。在这种情况下,'&lt;U1' 代表宽度为 1 字节的无符号整数数据,也就是 c 风格语言中的 unsigned char。这是不可分割的,它与'%.4f' format specifier 不兼容。相反,请使用 '%u' 之类的内容。

np.savetxt('data.csv', array, delimiter=',', fmt='%u')

如果你真的希望你的数据在你的 csv 中格式化为浮点值,你可以cast 数组像这样浮动:

np.savetxt('data.csv', array.astype(float), delimiter=',', fmt='%.4f')

【讨论】:

    猜你喜欢
    • 2021-10-30
    • 1970-01-01
    • 1970-01-01
    • 2015-04-24
    • 1970-01-01
    • 2021-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多