0

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)
zooted
  • 165
  • 1
  • 1
  • 12
  • possible duplicate of [Skip the headers when editing a csv file using Python](http://stackoverflow.com/questions/14257373/skip-the-headers-when-editing-a-csv-file-using-python) – Denilson Sá Maia Aug 05 '14 at 20:22

2 Answers2

1

Add this before the loop:

# Python 2.x
writer.writerow(reader.next())

# Python 3.x
writer.writerow(next(reader))

It will get the first line and return it. And then you are writing it to the output.

However, in my opinion you should make the code inside the loop resistant to non-numbers on that column (like in Al.Sal answer).

Denilson Sá Maia
  • 47,466
  • 33
  • 109
  • 111
0

You could use an exception handler. The try is incredibly cheap; since you'd only have one header, the more expensive except won't get called enough to impact performance. Also, you would have a good way to handle non-number rows later on.

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
    try:
        row[5] = '%04d'%int(stripped_value)
    except ValueError: 
        pass # Or do something, to avoid passing it silently  
    writer.writerow(row)

Your code snippet with correct indentation:

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)

    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
        try:
            row[5] = '%04d'%int(stripped_value)
        except ValueError: 
            pass # Or do something, to avoid passing it silently  
        writer.writerow(row)
Al.Sal
  • 984
  • 8
  • 19
  • Not sure why I'm getting this error. ValueError: I/O operation on closed file – zooted Aug 05 '14 at 20:13
  • Right. Make sure everything is properly indented. I'll update my answer in a second, @Nick – Al.Sal Aug 05 '14 at 20:15
  • Instead of fixing the code snippet by copying it into the answer, edit the question itself. – Denilson Sá Maia Aug 05 '14 at 20:20
  • @DenilsonSá oh really? I was thinking it's best to preserve the original question and I got to integrate my solution into the entire code snippet. Is it better style on SO to just edit the question if there's an indentation problem? – Al.Sal Aug 05 '14 at 20:21
  • It worked when I wrote it to a .txt file, however it didn't work when I wrote it to the csv file. It actually odd. I'm not sure why its doing that now. – zooted Aug 05 '14 at 21:12
  • @Nick Sure. You're trying to convert a `string` into an `int`; the program doesn't like that, so it tells you something wrong happened (there's an exception.) Instead of stopping the program like it did before, you tell the program how to handle it. They're pretty useful, but I wouldn't use a `try... except` as my first line of defense. I'd prefer to rework the problem so I didn't have to worry about it. In this case, it is fairly clean and handles the other case (where you have a non-number entry besides the header; that is, in a place you don't expect) – Al.Sal Aug 05 '14 at 21:16
  • Also, make sure you keep an eye on the indentation. You had the `IOError` because you were out of the `with` block - once you're out of the `with` block in a function or script, it closes the file! Do some research on writing functions to find out more. – Al.Sal Aug 05 '14 at 21:17
  • @Nick what's the error? Can you update your question? – Al.Sal Aug 05 '14 at 21:18
  • Hi Al its working, just wondering why its not displaying within the csv file. It does display within a .txt file, and within the python shell when I print it out. – zooted Aug 06 '14 at 20:28
  • @Nick Are you sure you're passing the file names in the `with...` lines? – Al.Sal Aug 07 '14 at 13:40