Ruby: How can I process a CSV file with “bad commas”?

后端 未结 4 1324
野趣味
野趣味 2021-01-23 16:10

I need to process a CSV file from FedEx.com containing shipping history. Unfortunately FedEx doesn\'t seem to actually test its CSV files as it doesn\'t quote strings that have

相关标签:
4条回答
  • 2021-01-23 16:43

    If you are so lucky as to only have one field like that, you can parse the leading fields off the start, the trailing fields off than end and assume whatever is left is the offending field. In python (no habla ruby) this would look something like:

    fields = line.split(',') # doesn't work if some fields are quoted
    fields = fields[:5] + [','.join(fields[5:-3])] + fields[-3:]
    

    Whatever you do, you should be able at a minimum determine the number of offending commas and that should give you something (a sanity check if nothing else).

    0 讨论(0)
  • 2021-01-23 16:44

    Well, here's an idea: You could replace each instance of comma-followed-by-a-space with a unique character, then parse the CSV as usual, then go through the resulting rows and reverse the replace.

    0 讨论(0)
  • 2021-01-23 16:53

    Perhaps something along these lines..

    using gsub to change the ', ' to something else

    ruby-1.9.2-p0 > "foo,bar,baz,pop, blah,foobar".gsub(/,\ /,'| ').split(',')
    [
        [0] "foo",
        [1] "bar",
        [2] "baz",
        [3] "pop| blah",
        [4] "foobar"
    ]
    

    and then remove the | after words.

    0 讨论(0)
  • 2021-01-23 16:56

    you can use a negative lookahead

    >> "foo,bar,baz,pop, blah,foobar".split(/,(?![ \t])/)
    => ["foo", "bar", "baz", "pop, blah", "foobar"]
    
    0 讨论(0)
提交回复
热议问题