I have a folder CSV files containing lines as follows:
Date | A | B | C |
2021-10-11| A | B | C |
I would like to rearrange the date column across every file, for instance, from 2021-10-11 to 10/11/2021.
I have a folder CSV files containing lines as follows:
Date | A | B | C |
2021-10-11| A | B | C |
I would like to rearrange the date column across every file, for instance, from 2021-10-11 to 10/11/2021.
To import your data use the csv module and flip the dates you will need a csv reader and a csv writer. Using the DictReader and DictWriter classes can be convenient.
Converting dates is pretty simple using the datetime module.
import csv
from datetime import datetime
old_fmt = '%Y-%m-%d'
new_fmt = '%m/%d/%Y'
def process_file(path):
# read the data in
with path.open('r') as f:
reader = csv.DictReader(f)
fieldnames = reader.fieldnames
data = list(reader)
# write the data out
with path.open('w') as f:
writer = csv.DictWriter(f, fieldnames=fieldnames)
writer.writeheader()
for row in data:
# here we convert the format
dt = datetime.strptime(row['date'], old_fmt)
row['date'] = dt.strftime(new_fmt)
writer.writerow(row)
pathlib is a great module for working with the filesystem. Its easy to process all the files.
from pathlib import Path
folder = Path('path/to/directory')
for path in folder.iterdir():
if path.is_file():
process_file(path)