Skip to content

Instantly share code, notes, and snippets.

@angrychimp
Created June 17, 2020 17:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save angrychimp/ca5737cb08d957538571a25ece9ebf68 to your computer and use it in GitHub Desktop.
Save angrychimp/ca5737cb08d957538571a25ece9ebf68 to your computer and use it in GitHub Desktop.
Fix CSV files with array data
import csv
import os
import sys
sourcefile = os.path.expanduser(sys.argv[1])
targetfile = sourcefile.replace('.', '-fixed.', 1)
with open(sourcefile) as csvfile, open(targetfile, 'w') as fixfile:
reader = csv.DictReader(csvfile)
writer = csv.DictWriter(fixfile, reader.fieldnames)
writer.writeheader()
for row in reader:
for k,v in row.items():
if v and v[0] == '[' and v[-1] == ']':
row[k] = row[k].replace("'",'"')
writer.writerow(row)
@angrychimp
Copy link
Author

Requires Python 3.5+

Download the file, then execute:

python3 ./fix-csv-array.py ~/source-file.csv

This script will create a new file (e.g. source-file-fixed.csv) in the same folder as the source. It will correct any arrays that have single-apostrophes wrapping values, converting them to proper JSON-encoded arrays.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment