Skip to content

Instantly share code, notes, and snippets.

@bast
Last active January 15, 2020 11:24
Show Gist options
  • Save bast/ab436984d8ea9db994f145aedbb1a7e8 to your computer and use it in GitHub Desktop.
Save bast/ab436984d8ea9db994f145aedbb1a7e8 to your computer and use it in GitHub Desktop.
import csv
import sys
import click
@click.command()
@click.option('--file-name', help='Input file (file is not modified).')
@click.option('--index', help='Insert column at this position (starts at 0).')
@click.option('--column-name', help='Column name.')
def main(file_name, index, column_name):
with open(file_name, 'r') as f:
reader = csv.reader(f)
writer = csv.writer(sys.stdout)
for i, row in enumerate(reader):
if i == 0:
row.insert(int(index), column_name)
else:
row.insert(int(index), '')
writer.writerow(row)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment