简体   繁体   中英

Export to CSV file Python

I want to export the data to a CSV file but the output produced is different from what I want

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)

output from this code

c,o,n,t,o,h

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

i want the output like this

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

i want contoh and the number array to be in one row

please help me

This is what you want to achieve, add a word to the numpy array and then save it as a csv file.

See a mock up below. Let me know if it works.

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()

Result below (in samp.csv ):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM