csv - How to .write() two items, a zero and an iterator in Python? -
i may have simple formatting question here - hrout.write('0',i) in order add/preface 0 'i' timestamp, don't know correct syntax. code below. thank all.
hrin = open('hrsinput.csv') hrout = open('hrsoutput.csv', 'wt') in hrin: if len(i) < 5: hrout.write('0', i) else: hrout.write(i) hrin.close() hrout.close()
** found padding technique works. may have been tricked excel because in notepad padding shows up.
hrin = open('hrsinput.csv') hrout = open('hrsoutput.csv', 'wt') in hrin: hrout.write("{}\n".format(i.rstrip().zfill(5))) hrin.close() hrout.close()
use str.format:
hrout.write('0{}'.format(i))
or remove if/else , pad:
for in hrin: hrout.write("{}\n".format(i.rstrip().zfill(5)))
zfill add 0 times 4 characters:
in [21]: "12:33".zfill(5) out[21]: '12:33' in [22]: "2:33".zfill(5) out[22]: '02:33'
Comments
Post a Comment