I have python code to edit a column in a csv file. It removes the zeros from integers in row 5. And then it adds a zero if the integer is 3 or less so it has a total of 4 integers or more.
The problem I'm having it doesn't like the title row which is not an integer. Does anyone know how I keep the header but adjust the code so that it doesn't look at the first line of the csv file.
Here is the code:
import csv
import re
import os
import sys
with open('', 'r') as infile, open('', 'w') as outfile:
reader = csv.reader(infile)
writer = csv.writer(outfile)
#firstline = True
#for row in outfile:
# if outfile:
# firstline = False
for row in reader:
# strip all 0's from the front
stripped_value = re.sub(r'^0+', '', row[5])
# pad zeros on the left to smaller numbers to make them 4 digits
row[5] = '%04d'%int(stripped_value)
writer.writerow(row)