python - Problem in spliting columns in a row if it contains semicolon and double quote in text -
i want import few rows csv file. problem there few columns contain semicolon , double quote in middle of text.
and since delimeter ; , csv quote ", splits columns sees ; , " in middle of text.
my sample csv file :
"hello";"<span onmouseup="__dopostback('bb','')">;</span> <span onmouseup="__dopostback('j','')" style="display: none" enabled="true"> ";"bye"
the code read rows are:
csv.reader((line.replace('\0','') line in f) , delimiter=';',quotechar = '"') row in reader: print row , prints ;['hello', "<span onmouseup=__dopostback('bb','')>", '</span> <span onmouseup="__dopostback(\'j\',\'\')" style="display: none" enabled="true"> "', 'bye']
i want result :
row[0] = hello row[1] = <span onmouseup="__dopostback('bb','')">;</span> <span onmouseup="__dopostback('j','')" style="display: none" enabled="true"> row[2] = bye
wheras output is:
row[0] = hello row[1] = <span onmouseup="__dopostback('bb','')"> row[2] = </span> <span onmouseup="__dopostback('j','')" style="display: none" enabled="true"> row[3] = bye
i have used code " reader = csv.reader(open("yourfile.csv", "rb"), delimiter=';') "
defined in python split function, still code splits rows 4.
any appreciated.
thank you..
i think have problem input format, not proper csv:
quoted wikipedia:
fields contain special character (comma, newline, or double quote), must enclosed in double quotes. [...] if field's value contains double quote character escaped placing double quote character next it
it seems input file misses out on escaping double quotes.
that said, if cannot input, have come kind of pattern in data allow fix file before passing cvs.reader
, or have parse manually according said patterns. can complex quickly.
Comments
Post a Comment