Adding header line to export csv in Python -
i want add header line final output csv python. how can that? first removing header of main file items.csv
because @ end python adds 'target'
word header too. after adding word want write new header.
here code:
import csv itertools import islice import os open('items.csv') sample, open('items_50.csv', "w") out: csv1=csv.reader(sample) header = next(csv1, none) csv_writer = csv.writer(out) sirali = sorted(csv1, key=lambda x:int(x[0]), reverse=true) csv_writer.writerows(islice(sirali,50)) file1=open('items_50.csv','rb') readfile=csv.reader(file1,) file2=open('final.csv','wb') writefile=csv.writer(file2,delimiter='"') result=() row in readfile: result= [row[0]+','+row[1]+','+row[2]+','+row[3]+'target'] writefile.writerow(result) file1.close() file2.close()
an example final.csv right now:
0,apple,orange,gettarget 25,steven,jack,sendtarget 33,pencil,book,sendtarget 8,notebook,cellphone,gettarget
i want make this:
count,thing,owner,action 0,apple,orange,gettarget 25,steven,jack,sendtarget 33,pencil,book,sendtarget 8,notebook,cellphone,gettarget
thank you.
do befor for
loop:
writefile.writerow(["count", "thing", "owner", "action"])
btw: can do
for row in readfile: row[3] += 'target' writefile.writerow(row)
Comments
Post a Comment