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

后端 未结 4 1357
野趣味
野趣味 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: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.

提交回复
热议问题