'How to rename key/header in csv DictReader
For instance given mycsv.csv
file
h1,h2
a,b
c,d
how to rename h2
onto HTwo
with reader
reader = csv.DictReader(open('mycsv.csv'))
(Additionally how to write the csv file back with the updated header.)
Note approaches with awk are also valued.
Solution 1:[1]
For a simple CSV file (one that contains no quoted strings), awk
is a fine solution, and Brian's answer will work fine. If your CSV file contains quoted strings that themselves contain commas, like this:
h1,h2
"this, is, value, 1",value2
...then awk
will fall over.
If you want to do this in Python, there's no reason to use DictReader
. Something like this will work:
import csv
with open('mycsv.csv') as infd, open('mycsv.out', 'w') as outfd:
reader = csv.reader(infd)
writer = csv.writer(outfd)
# read the header
header = next(reader)
# modify the column title
header[1] = 'hTwo'
# write the new header out
writer.writerow(header)
# copy all other rows unmodified
for row in reader:
writer.writerow(row)
# and then rename mycsv.out to mycsv.csv if you wish.
Solution 2:[2]
One way to solve it is to use pop()
reader = csv.DictReader(open('mycsv.csv'))
for d in reader:
d['HTwo'] = d.pop('h2')
Solution 3:[3]
awk oneliner:
awk -F, -v OFS=, 'NR==1 && $2=="h2" {$2="hTwo"};1' file
The above is very specific: it says "if the first line contains 'h2' in the second field, change it to 'hTwo'"
Solution 4:[4]
inplace replacement with sed
$ sed -i '1s/h2/HTwo/' mycsv.csv
Solution 5:[5]
Using CSV DictReader:
Change headers of CSV file and copy the contents to another file:
import csv
with open('old_file.csv','r') as file:
reader = csv.DictReader(file)
# read the orignal headers ["head1", "head2"]
print("old headers list", reader.fieldnames)
# set new_headers_list []
new_headers_list = ["new_head1", "new_head2"]
reader.fieldnames = new_headers_list
rows = list(reader)
with open("new_file", "w") as new_file:
writer = csv.DictWriter(new_file, fieldnames=new_headers_list)
# write the new header out
writer.writeheader()
# copy all other rows unmodified
writer.writerows(rows)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | larsks |
Solution 2 | Tzar |
Solution 3 | Brian |
Solution 4 | karakfa |
Solution 5 | Adarsh Sethi |