将 numpy 保存为 csv 代码时显示错误

Posted

技术标签:

【中文标题】将 numpy 保存为 csv 代码时显示错误【英文标题】:while saving numpy as csv code shows error 【发布时间】:2020-09-19 17:46:22 【问题描述】:

将以下 NumPy 数组保存为 CSV 时出现错误

print(stfinal)

['70.6' '68.7' '67.8' '67.0' '68.7' '67.7' '66.9' '71.1' '73.0' '72.7'
 '74.2' '72.9' '74.5' '73.8' '68.5' '67.4' '70.0' '72.5' '72.0' '69.3'
 '65.9' '66.2' '65.8' '68.1' '69.7' '69.9' '70.5' '70.1' '70.0' '70.2'
 '72.4' '60.0' '54.6' '54.4' '48.7' '52.4' '53.4' '58.3' '61.4' '63.4'
 '63.0' '64.8' '65.2' '65.6' '63.5' '58.6' '54.1' '58.6' '60.3' '61.0'
 '56.1' '50.0' '45.0' '44.5' '52.4' '56.5' '56.4' '57.0' '57.4' '55.7'
 '52.9' '53.7' '72.6' '65.0' '65.4' '58.7' '60.1' '64.5' '76.3' '73.2'
 '73.9' '74.5' '75.0' '78.9' '75.8' '72.8' '73.4' '65.9' '70.1' '67.8'
 '70.3' '66.6' '60.9' '53.2' '57.1' '63.2' '65.5' '65.5' '65.7' '67.7'
 '64.6' '60.0' '56.2' '7.5' '5.6' '3.7' '4.0' '4.4' '5.0' '5.5' '6.3'
 '7.7' '6.7' '6.9' '4.5' '5.4' '7.3' '7.5' '6.6' '6.2' '6.1' '6.6' '4.3'
 '3.9' '3.9' '3.9' '4.3' '4.9' '5.6' '6.2' '6.7' '5.9' '4.6' '5.4' '26.9'
 '26.9' '26.9' '26.9' '26.9' '26.9' '26.9' '26.9' '26.9' '26.9' '26.9'
 '27.0' '27.0' '27.0' '27.0' '27.1' '27.0' '27.0' '26.9' '27.0' '26.9'
 '26.9' '26.9' '26.9' '26.9' '26.9' '26.9' '27.0' '27.0' '27.0' '27.0']


  numpy.savetxt(f"monthyear.csv",stfinal, delimiter=",")

在以上述方式保存时,我得到了错误

【问题讨论】:

您是否阅读过savetxtfmt 参数?你了解数组的 dtype 是什么吗? @hpaulj 老实说,我不知道这些是什么意思,这只是我接触 python 或任何 programmnig 语言的第一个月 这应该可以工作:np.savetxt('file.csv', l, delimiter = ', ', fmt = '%s') 这能回答你的问题吗? TypeError: Mismatch between array dtype ('object') and format specifier ('%.18e') 【参考方案1】:

我找到了答案 我只需要将所有元素转换为浮点数,同时将其存储在 numpy 中

【讨论】:

以上是关于将 numpy 保存为 csv 代码时显示错误的主要内容,如果未能解决你的问题,请参考以下文章

当我在android studio中运行此代码时显示错误?

将数据从CSV转换为numpy数组时出错

GIS中shapfile文件的属性表连接Excel表时显示:连接数据库失败,出现基础数据库错误,没有注册类。

核心数据:ID 已设置,但保存时显示为 nil

保存时显示Flycheck缓冲区(有错误)

如何将 numpy 数组写入 csv 文件?