Write a CSV based on another CSV file creating an additional empty row?

34 Views Asked by At

I have a system that creates csv files nightly and sends them off to a SMTP server.

Customer has requested that we make a separate text file with differently formatted timestamps, this time to ISO format, and send it to a separate SMTP server. Due to some poor programming, constraints and prior technical debt it is much easier for me to let the system go on with it's normal process, but read the created CSV, modify the time stamp columns to the right format, and create the new csv like that and then send it off.

I almost have it working but for some reason my function is adding an additional empty row in between every row.

My function:

def convertFileToIsoFormat(read_filePath, write_filePath):
    import csv
    import datetime
    from __future__ import with_statement
    with open(read_filePath) as csvfile:
        reader = csv.reader(csvfile, delimiter='\t')
        for row in reader:
            modifiedRow = row
            columnsToModify = [4,7,11]
            for column in columnsToModify:
                modifiedRow[column] = datetime.datetime.strptime(modifiedRow[column], '%Y-%m-%d %H:%M:%S').isoformat()
            # Special column with different format
            modifiedRow[22] = datetime.datetime.strptime(modifiedRow[21], '%m/%d/%Y %H:%M:%S %p').isoformat()
            with open(write_filePath, 'a') as newcsv:
                writer = csv.writer(newcsv, delimiter='\t')
                writer.writewrow(modifiedRow)

This works in making the file, and in Notepad on this Windows Server 2012 the file looks right, but in my newer Windows 10 notepad I see the extra rows, and in NotePad++ on either computer I see the extra rows, so I suspect it has something to do with what the linebreak character is supposed to be for the os but I don't know how to work with that here.

Using python (technically jython) 2.5 for this. Any thoughts on what I am missing?

0

There are 0 best solutions below