How can I read CSV with strange quoting in ruby? -


i have csv file line like:

col1,col "two",col3 

so illegal quoting error , fix setting :quote_char => "\x00"

["col1", "col\"two\"", "col3"] 

but there line like

col1,col2,"col,3" 

later in file

["col1", "col2", "\"col", "3\""] 

then read file line line , call parse_csv wrapped in block. set :quote_char => "\"", rescue csv::malformedcsverror exceptions , particular lines set :quote_char => "\x00" , retry

all works until line

col1,col "two","col,3" 

in case rescues exception, set :quote_char => "\x00" , result is

["col1", "col\"two\"", "\"col", "3\""] 

apple numbers able openn file absolutely correctly.

is there setting parse_csv handle without preprocess string in way?

upd show csv lines in file , results (arrays) printed p. there no actual \" in strings.

this invalid csv file. if have access source, (ask to) generate data follows:

col1,"col ""two""","col,3" 

if not, option parse data yourself:

pseudocode:  while(read_line) {      bool insidequotes = false     each_char_in_line {          if(char == doublequote)             insidequotes = !insidequotes           if(char == ',' , !insidequotes)             // separator found - process field     } } 

this take care of escaped quotes in col1,"col ""two""","col,3".

if file contains multiline fields, more work has done.


Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -