How do I alter the column headers in a .csv file?

Hi all,

any good tips for setting up an automated flow for editing the headers in a .csv file? The file name is the same every time, except for a timestamp-part.

I need to change/translate the headers. They are always the same in the source file, and they always need be changed to the same in the output file. The one thing I can’t say for sure is if the order of the “columns” will always be the same, so a search/replace is probably needed.

No further alterations are needed.

I have Hazel and Alfred available.


You’re a bit too vague for me to address your problem. That being said here’s a python script which:

  • finds all files with a timestamp
  • changes a header
  • writes to a new file.
import csv
from glob import glob

# Searches for a file with numbers 
for i in glob("ex-[0-9]*.csv"):
    rows = []
    with open(i, "r") as f:
        reader = csv.DictReader(f)
        for row in reader:
            # renames 'left' to 'leeft'
            row["leeft"] = row["left"]
            row.pop("left", None)
    # Writes to new file
    with open("new.csv", "w") as f:
        writer = csv.DictWriter(f, fieldnames=rows[0].keys())

As a repl

1 Like