Created
June 17, 2020 17:37
-
-
Save angrychimp/ca5737cb08d957538571a25ece9ebf68 to your computer and use it in GitHub Desktop.
Fix CSV files with array data
This file contains hidden or 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
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Requires Python 3.5+
Download the file, then execute:
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.