Skip to content

Instantly share code, notes, and snippets.

@larrasket
Last active December 30, 2022 13:51
Show Gist options
  • Save larrasket/acfe1c5ebc03842a1b37b6c2edd6083c to your computer and use it in GitHub Desktop.
Save larrasket/acfe1c5ebc03842a1b37b6c2edd6083c to your computer and use it in GitHub Desktop.
Python script to add a column to cvs file with a default value
#!/usr/bin/env python3
import csv
import sys
# Read command line arguments
field_name = sys.argv[1]
default_value = sys.argv[2]
# Open the input CSV file
with open('input.csv', 'r') as input_file:
# Create a CSV reader to parse the input file
reader = csv.reader(input_file)
# Open the output CSV file
with open('output.csv', 'w', newline='') as output_file:
# Create a CSV writer for the output file
writer = csv.writer(output_file)
# Write the header row
header_row = next(reader)
header_row.append(field_name)
writer.writerow(header_row)
# Write the data rows
for row in reader:
row.append(default_value)
writer.writerow(row)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment