Start New Discussion within our Software Development Community

Hello I need to add a zero in front of a number if it is 3 or less so that it has 4 digits total. I only need this for row 6 or row 5 to be python specifc. I need this to write to the same csv file.

AFI12001 1 S- 201404 P 1330 2 0.00875 US 40
AFI12001 1 S- 201404 P 1340 2 0.01125 US 50
AFI12001 1 S- 201404 P 1350 2 0.01375 US 70
AFI12001 1 S- 201405 P 1150 2 0.005 US 20
AFI12001 1 W- 201404 C 690 2 0.04875 US 5
AFI12001 1 W- 201404 C 700 2 0.03625 US 5
AFI12001 1 W- 201404 P 530 2 0.00125 US 40
AFI12001 1 W- 201404 P 540 2 0.00125 US 5
AFI12001 1 W- 201404 P 550 2 0.00125 US 1
AFI12001 1 W- 201404 P 600 2 0.01 US 15
AFI12001 1 W- 201404 P 610 2 0.01875 US 55

import csv

new_rows = []
with open('csvpatpos.csv','r') as f:
    csv_f = csv.reader(f)
    for row in csv_f:
        row = [ x.zfill(2) for x in row ]
        new_rows.append(row)

with open('csvpatpos.csv','wb') as f:
    csv_f = csv.writer(f)
    csv_f.writerows(new_rows)

I'm currently having trouble getting this to display correctly.