使用np.savetxt将数组另存为csv列



我有一个numpy数组,如下所示:

new_data_smooth = [1,2,3,....,2800]

我用这个代码来保存数组:

with open("label.csv", "w+") as f:
np.savetxt(f,(new_data_smooth),delimiter=',', fmt='%3f',comments='')

我想把它保存到.csv文件中,这样文件看起来像:

例如:

1,2,3,....,2800

https://i.stack.imgur.com/w6ptO.jpg

但它给了我这个:

例如:

1
2
3
...
2800

https://i.stack.imgur.com/5gV5q.jpg

有没有一种方法可以使用np.savetxt只保存一行?

您可以添加两个列表来创建一个列表,并将其写入磁盘

np.savetxt(f,new_data_smooth+new_data,delimiter=',', fmt='%3f',comments='')

或者使用换行参数:

np.savetxt(f,(new_data_smooth,new_data),delimiter=',', fmt='%3f',comments='',newline=',')

但是使用换行参数,您将在文件的末尾使用逗号

最新更新