newlines in double quotes in reading CSV in python -
i have csv file in following format:
"4931286","lotion","new york","bright color, yellow 5" long 20% nylon" "931286","shampoo","new york","dark, yellow 10" long 20% nylon" "3931286","conditioner","la","bright color, yellow 5" long 50% nylon"
the above data should read 3 rows 4 columns: id, product name, location, , description. can seen, there newlines within descriptions each row.
i've been searching other related stackoverflow questions none of solutions seem solve issue.
here attempt:
from stringio import stringio file = stringio("""4931286","lotion","new york","bright color, yellow\n 5" long 20% nylon""") row in csv.reader(file,quotechar='"', delimiter=',',quoting=csv.quote_all, skipinitialspace=true): print row
and results following:
['4931286"', 'lotion', 'new york', 'bright color, yellow 5 long'] ['20% nylon']
but, want
['4931286"', 'lotion', 'new york', 'bright color, yellow 5 long 20% nylon']
how achieve this? there should way in python?
the data not in csv format.
"
in csv must escaped \
"bright color, yellow\n 5\" long 20% nylon"
.
if "
used inches (prefixed number) try this:
import re data = re.sub(r'([0-9])"(?![,\n])', r'\1\\"', data)
this regex replace "
\"
if prefixed number
and parse data csv.reader
edit: changed regex because of maxu's suggestion.
Comments
Post a Comment