Ruby CSV读取多行字段 [英] Ruby CSV read multiline fields

查看:457
本文介绍了Ruby CSV读取多行字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I exported tables and queries from SQL, where some of the fields are multi-line.

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

The Ruby (1.9+) way to read CSV appears to be:

require 'csv'

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

如果我的数据是这样的,

Which works great if my data is like this:

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

(第一行是标题/属性)

(The first line is the header/attributes)

但如果我有:

"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)

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

This seems to be because the end of the line doesn't have a close quote, as it spans several lines.

(我试过'FasterCSV',那个gem自ruby 1.9以来就变成了'csv')

(I tried 'FasterCSV', that gem became 'csv' since ruby 1.9)

推荐答案

而不是多行但格式错误的CSV。

Your problem is not the multiline but malformed CSV.

在行尾之后替换 \并结束空格,如下所示:

Replace the \" and end space after a line end like this:

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。我在这里使用 __ ,但您可以使用其他不冲突的字符。

If you have no control over the program that delivers the CSV, you have to open the file, read the contents, do a replace and then parse the CSV. I use __ here but you can use other non-conflicting characters.

这篇关于Ruby CSV读取多行字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆