Ruby CSV读取多行字段

时间:2012-10-16 13:05:53

标签: mysql ruby csv fastercsv

我从SQL导出了表和查询,其中一些字段是多行的。

Ruby(1.9+)读取CSV的方式似乎是:

require 'csv'

CSV.foreach("exported_mysql_table.csv", {:headers=>true}) do |row|
    puts row
end

如果我的数据是这样的,那么效果很好:

"id","name","email","potato"
1,"Bob","bob@bob.bob","omnomnom"
2,"Charlie","char@char.com","andcheese"
4,"Doug","diggyd@diglet.com","usemeltattack"

(第一行是标题/属性)

但如果我有:

"id","name","address","email","potato"
1,"Bob","--- 
- 101 Cottage row
- Lovely Village
- \"\"
","bob@bob.bob","omnomnom"
2,"Charlie","--- 
- 102 Flame Street
- \"\"
- \"\"
","char@char.com","andcheese"
4,"Doug","--- 
- 103 Dark Cave
- Next to some geo dude
- So many bats
","diggyd@diglet.com","usemeltattack"

然后我收到错误:

.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/csv.rb:1894:in `block (2 levels) in shift': Missing or stray quote in line 2 (CSV::MalformedCSVError)

这似乎是因为该行的末尾没有一个接近的引号,因为它跨越了几行。

(我尝试过#FasterCSV',宝石成为' csv'自ruby 1.9以来)

1 个答案:

答案 0 :(得分:5)

您的问题不是多线,而是格式错误的CSV。

在行结束后替换\"和结束空格,如下所示:

require 'csv' 

ml = %q{"id","name","address","email","potato" 
1,"Bob","---  
- 101 Cottage row 
- Lovely Village 
- \"\" 
","bob@bob.bob","omnomnom" 
2,"Charlie","---  
- 102 Flame Street 
- \"\" 
- \"\" 
","char@char.com","andcheese" 
4,"Doug","---  
- 103 Dark Cave 
- Next to some geo dude 
- So many bats 
","diggyd@diglet.com","usemeltattack"}

ml.gsub!(/\" \n/,"\"\n").gsub!(/\\\"/,"__")

CSV.parse(ml, {:headers=>true}) do |row|
  puts row
end

这给出了:

"id","name","address","email","potato"
1,"Bob","---  
- 101 Cottage row 
- Lovely Village 
- ____
","bob@bob.bob","omnomnom"
etc

如果您无法控制传送CSV的程序,则必须打开文件,阅读内容,进行替换,然后解析CSV。我在这里使用__,但您可以使用其他非冲突字符。