Skip to content

Instantly share code, notes, and snippets.

@noam1023
Created May 7, 2019 06:43
Show Gist options
  • Save noam1023/26b4eb85cb525efee9b8fd728f255f26 to your computer and use it in GitHub Desktop.
Save noam1023/26b4eb85cb525efee9b8fd728f255f26 to your computer and use it in GitHub Desktop.
preprocess csv file to remove commas appearing in ""
# remove comma char in strings in a csv file without harming the rest of the content.
# read input from supplied file name or from stdin
# write to stdout
# example:
# cat t.csv | python drop_comma.py
# python drop_comma.py t.csv
# Noam Cohen, 2019-05-05
# tested with python 3.6
import csv
import sys
if len(sys.argv) == 2:
fn=open(sys.argv[1])
else:
fn=sys.stdin
reader = csv.reader(fn, delimiter=',', quotechar='"')
for row in reader:
# remove comma in a string
cleaned = map(lambda x: x.replace(",",""), row)
print(", ".join(cleaned))
if len(sys.argv) == 2:
fn.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment