0

On a weekly basis, I need to replace the header in a csv file (that has a date dependent name) and delete two of the columns. I though the easiest way would be to write a new csv file with the pertinent information(i.e. without columns k and l). This is how my code looks like:

import csv
import calendar
import datetime
from datetime import date, timedelta

today = date.today()
tuesday = date.today() - timedelta(3)
p = tuesday.strftime('%Y%m%d') 

us_csv = 'E:/' + "TEST_us_" + p + ".csv"

HIn = "a, b, c, d, e, f, g, h, k, l"
HOut = "A, B, C, D, E, F, G, H"

fIn= open ('us_csv', 'r')

HeaderIn = fIn.readline()
HeaderOut = HeaderIn.replace(HIn, HOut, 1)

fOut = open ('E:/Abase/usStats.csv', 'w')

fOut.write(HeaderOut + '\n')
    for line in fIn
    fOut.write(line)
fOut.close

The new csv is empty. I read most of the similar questions, but I simply can't figure out how to do this. Any help would be greatly appreciated. Thank you.

2
  • Is the spacing in your example correct? Your for loop looks indented for no reason (it's not within a control statement of any kind), and your fOut.write(line) is not indented further than the for, which means it would execute after the loop completed? Also, is this all of the code or just a relevant snippet? Commented Feb 27, 2015 at 20:55
  • No, the indent just copied incorrectly. And this is just a snippet of a larger script - basically this csv is one of the inputs that will go to update a feature service in GIS. However, once I get to arcpy, I can probably finish what I need to do. Commented Feb 28, 2015 at 1:27

1 Answer 1

2

This works for me. I like to use writelines so I can do all the writing at once. It might be possible that you were running into trouble because you had two files open. I'm not sure. To be safe I always open and close files immediately using a block as shown.

This might not be necessary, but I tend to split up the rows into lists of values so I can do the manipulations I need. I'm using the csv module to this for me. You can see here I used list splicing to remove the last two columns of each row. Then I join them back together.

import os
import argparse
import operator
import csv

def main():
    p = argparse.ArgumentParser (description="Removes last two columns and renames headers.")
    p.add_argument("origfile", help="Path of original file")
    p.add_argument("newfile", help="Path of new file.")
    args = p.parse_args()

    with open(args.origfile) as f:
        raw_rows = f.read().splitlines()

    new_header_row = "A,B,C,D,E,F,G,H\n" # Don't put spaces

    # for easier manipulation I like to split the row into a list values
    # then rejoin them later after I've changed or removed what I needed
    rows = csv.reader(raw_rows)

    newfile_lines = [new_header_row]
    newfile_lines.extend(",".join(row[:-2]) + "\n" for row in rows)

    with open(args.newfile, 'w') as f:
        f.writelines(newfile_lines)



if __name__ == '__main__':
    main()

I ran this on:

a,b,c,d,e,f,g,h,i,j
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10

And got:

A,B,C,D,E,F,G,H
1,2,3,4,5,6,7,8
1,2,3,4,5,6,7,8
1,2,3,4,5,6,7,8
1,2,3,4,5,6,7,8
1,2,3,4,5,6,7,8
1,2,3,4,5,6,7,8
1,2,3,4,5,6,7,8
1,2,3,4,5,6,7,8
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.