导出到CSV文件Python

2024-04-18 15:24:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我想将数据导出到一个CSV文件,但输出结果与我想要的不同

word = "contoh"
vector = np.array([-0.6910377 ,  1.6553369 ,  0.6142262 ,  2.7324615 , -0.29642856])
list=(word,vector)
print(list)
print(len(vector))
ok=open('samp.csv','a')
a=csv.writer(ok)
a.writerows(list)

此代码的输出

c,o,n,t,o,h

-0.6910377 ,  1.6553369 ,  0.6142262 ,  2.7324615 , -0.29642856....

我想要这样的输出

contoh,-0.6910377 , 1.6553369 , 0.6142262 , 2.7324615 , -0.29642856...

我希望contoh和数字数组在一行中

请帮帮我


Tags: 文件csv数据lennpokopenarray
1条回答
网友
1楼 · 发布于 2024-04-18 15:24:26

这就是您想要实现的,向numpyarray添加一个单词,然后将其另存为csv文件。你知道吗

见下面的模型。让我知道它是否有效。你知道吗

import csv
word = "contoh"
vector = np.array([-0.6910377 ,  1.6553369 ,  0.6142262 ,  2.7324615 , -0.29642856])
mylist= np.append(word,vector) #add word to vector
ok=open('samp.csv','a')
a=csv.writer(ok,lineterminator='\n')
a.writerows([mylist])
ok.close()

结果如下(在samp.csv):

contoh,-0.6910377,1.6553369,0.6142262,2.7324615,-0.29642856

相关问题 更多 >