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

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

提交回复
热议问题