Python 3 - How to write specific row in same file csv? -
i want update fieldname called "done" in same file csv
this structure of file csv:
input:
email done 1@gmail.com 2@gmail.com 3@gmail.com 4@gmail.com
output:
email done 1@gmail.com 1 2@gmail.com 1 3@gmail.com 1 4@gmail.com 1
what want like:
import csv open(r'e:\test.csv','r', encoding='utf-8') f: reader = csv.dictreader(f,delimiter='|') row in reader: #do here# #then write "1" filename "done" f.close()
how ? thank ! :)
with simple change use simple pair of read , write files; read line, , write out addition.
in [6]: open('test.csv','r') input: ...: open('test1.csv','w') output: ...: header=input.readline() ...: output.write(header) ...: line in input: ...: output.write('%s %8d\n'%(line.strip(),1)) in [7]: cat test1.csv email done 1@gmail.com 1 2@gmail.com 1 3@gmail.com 1 4@gmail.com 1
you use csv reader , csv writer; numpy
user might tempted use loadtxt
, savetxt
pair, though mixing string , number values in array takes bit more know-how.
in new enough python put both opens in 1 context:
with open('test.csv','r') input, open('test1.csv','w') output: ...
Comments
Post a Comment