Last active
December 30, 2022 13:51
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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