replace headers in csv file and writing selected columns using python 2.7
Question: 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.


Answer: 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
    

