Ruby CSV解析使用转义引号的字符串 [英] Ruby CSV parsing string with escaped quotes

查看:321
本文介绍了Ruby CSV解析使用转义引号的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的CSV文件中有一行包含一些转义引号:

  173,Yukihiro \Ruby当我尝试解析它的时候,Ruby的CSV解析器是一个简单的解析器。 :

  require'csv'
CSV.foreach('my.csv',headers:true,header_converters:符号)do | row |
puts row
end

我收到此错误:

  ... / 1.9.3-p327 / lib / ruby​​ / 1.9.1 / csv.rb:1914: in shift':第122行中缺少或有偏差的报价(CSV :: MalformedCSVError)

围绕此错误?

解决方案

CSV支持转换器,我们通常可以使用它来按字段的内容,回到我们的代码。例如,可用于对行中的所有字段减去额外空格



不幸的是,转换器在行被分割成字段之后触发,并且在该步骤期间,CSV对嵌入式引号产生了疯狂,因此我们必须在行读取步骤之间,



这是我的示例CSV文件:

  ID,姓名,国家
173,Yukihiro \The Ruby Guy \Matsumoto,Japan

保存您的 CSV.foreach 方法,这是我的示例代码,解析它没有CSV疯狂:

  require'csv'
require'pp'

header = []
文件。 foreach('test.csv')do | csv_line |

row = CSV.parse(csv_line.gsub('\',''))。首先

如果header.empty?
= row.map(&:to_sym)
next
end

row = Hash [header.zip(row)]
pp row
row [:Name]

end

  {:ID =>173,:Name =>Yukihiro \The Ruby Guy\\ Matsumoto,:Country =>Japan} 
YukihiroThe Ruby GuyMatsumoto

我假设你想要一个哈希,因为你指定:headers 标志:

  CSV.foreach('my.csv',headers:true,header_converters::symbol)do | row | 


I have a line in my CSV file that has some escaped quotes:

173,"Yukihiro \"The Ruby Guy\" Matsumoto","Japan"

When I try to parse it the the Ruby CSV parser:

require 'csv'
CSV.foreach('my.csv', headers: true, header_converters: :symbol) do |row|
  puts row
end

I get this error:

.../1.9.3-p327/lib/ruby/1.9.1/csv.rb:1914:in `block (2 levels) in shift': Missing or stray quote in line 122 (CSV::MalformedCSVError)

How can I get around this error?

解决方案

CSV supports "converters", which we can normally use to massage the content of a field before it's passed back to our code. For instance, that can be used to strip extra spaces on all fields in a row.

Unfortunately, the converters fire off after the line is split into fields, and it's during that step that CSV is getting mad about the embedded quotes, so we have to get between the "line read" step, and the "parse the line into fields" step.

This is my sample CSV file:

ID,Name,Country
173,"Yukihiro \"The Ruby Guy\" Matsumoto","Japan"

Preserving your CSV.foreach method, this is my example code for parsing it without CSV getting mad:

require 'csv'
require 'pp'

header = []
File.foreach('test.csv') do |csv_line|

  row = CSV.parse(csv_line.gsub('\"', '""')).first

  if header.empty?
    header = row.map(&:to_sym)
    next
  end

  row = Hash[header.zip(row)]
  pp row
  puts row[:Name]

end

And the resulting hash and name value:

{:ID=>"173", :Name=>"Yukihiro \"The Ruby Guy\" Matsumoto", :Country=>"Japan"}
Yukihiro "The Ruby Guy" Matsumoto

I assumed you were wanting a hash back because you specified the :headers flag:

CSV.foreach('my.csv', headers: true, header_converters: :symbol) do |row|

这篇关于Ruby CSV解析使用转义引号的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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