What's a semantically-correct way to parse CSV from SQL Server 2008?

前端 未结 2 790
无人共我
无人共我 2021-01-21 05:04

I got a CSV dump from SQL Server 2008 that has lines like this:

Plumbing,196222006P,REPLACE LEAD WATER SERVICE W/1\" COPPER,1996-08-09 00:00:00
Construction,1971         


        
2条回答
  •  灰色年华
    2021-01-21 05:19

    If your CSV doesn't ever use a double quote as a legitimate quoting character, tweak the options to CSV to pass :quote_char => "\0" and then you can do this (wrapped strings for clarity)

    1.9.3p327 > puts 'Construction,197133031B,"MORGAN SHOES" ALT,
                      1997-05-13 00:00:00'.parse_csv(:quote_char => "\0")
    Construction
    197133031B
    "MORGAN SHOES" ALT
    1997-05-13 00:00:00
    
    1.9.3p327 > puts 'Plumbing,196222006P,REPLACE LEAD WATER SERVICE W/1" COPPER,
                      1996-08-09 00:00:00'.parse_csv(:quote_char => "\0")
    Plumbing
    196222006P
    REPLACE LEAD WATER SERVICE W/1" COPPER
    1996-08-09 00:00:00
    

提交回复
热议问题