How to write a number as text while writing in csv file in python -
import csv = ['679l', 'z60', '033u', '0003'] z = csv.writer(open("test1.csv", "wb")) z.writerow(a)
consider code above
output:
676l z60 33u 3
i need in text format as
676l z60 033u 0003
how that.
the python csv
module not treat strings numbers when writing file:
>>> import csv >>> stringio import stringio >>> = ['679l', 'z60', '033u', '0003'] >>> out = stringio() >>> z = csv.writer(out) >>> z.writerow(a) >>> out.getvalue() '679l,z60,033u,0003\r\n'
if seeing 3
in other tool when reading need fix tool; python not @ fault here.
you can instruct csv.writer()
put quotes around not number; could make clearer whatever reads csv column not numeric. set quoting
csv.quote_nonnumeric
:
>>> out = stringio() >>> z = csv.writer(out, quoting=csv.quote_nonnumeric) >>> z.writerow(a) >>> out.getvalue() '"679l","z60","033u","0003"\r\n'
but won't prevent excel treating column numeric anyway.
if loading excel don't use open feature. instead create new empty worksheet , use import feature instead. let designate column text rather general.
Comments
Post a Comment