如果第一行后跟一个空行,Ruby CSV不会找到文件头 [英] Ruby CSV doesn't find file headers if first line is followed by an empty one

查看:112
本文介绍了如果第一行后跟一个空行,Ruby CSV不会找到文件头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个奇怪的问题,我的脚本不能在一些CSV文件上工作。问题是以下,我需要访问列名称(文件头)来正确处理文件。我一直在使用以下(测试)代码列出标题:

I have encountered an odd issue where my script wouldn't work on some CSV files. Problem is the following, I need to access the column names (file headers) to properly process the file. I've been using the following (test) code to list the headers :

CSV file = CSV.read(argument, :headers => true)
if file.headers() == nil
    puts "According to the doc : Headers will not be used"
elsif file.headers() == true
    puts "According to the doc : Headers will be used, but not read yet"
else
    puts "Headers were read, this is an array : #{file.headers.to_s}"
end

现在,应用于第一个csv:

Now, applied to the first csv :

key,fr
edit,Éditer
close,Fermer

我得到:

Headers were read, this is an array : ["key", "fr"]

但是,如果我知道在标题和内容之间添加一个空行如下:

But, if I know add an empty line between the headers and the content itself, like so :

key,fr

edit,Éditer
close,Fermer

它不再工作:

Headers were read, this is an array : []

这让我很生气。这是预期的行为吗?有什么我能做的吗?我一直在阅读ruby CSV文件,但找不到任何东西。
它可能是一些csv文件规格我不知道?
在ruby 1.9.3和2.1.1上测试

This baffles me. Is this expected behavior ? Is there anything I can do about it ? I've been reading the ruby CSV doc but cannot find anything. Could it be some csv file specs I don't know about ? Tested on ruby 1.9.3 and 2.1.1

推荐答案

添加选项:skip_blanks =>

Add the option :skip_blanks => true when you are reading the CSV file.

file = CSV.read(path, :headers => true,:skip_blanks => true)

此选项:skip_blanks => true ,将跳过所有这样的空行,如果你有任何现在,你会得到正常的输出。

This option :skip_blanks => true, will skip all such blank lines if any present like you are having, and you will get the normal output.

这里是我的尝试同样:

require 'csv'

content = <<_
key,fr

edit,Éditer
close,Fermer
_

File.write('test',content)

file = CSV.read('test', :headers => true,:skip_blanks => true)
if file.headers() == nil
    puts "According to the doc : Headers will not be used"
elsif file.headers() == true
    puts "According to the doc : Headers will be used, but not read yet"
else
    puts "Headers were read, this is an array : #{file.headers.to_s}"
end
# >> Headers were read, this is an array : ["key", "fr"]

你说,我也同意你,下面一个:

Working correctly as you said, and I also agreed with you, below one :

require 'csv'

content = <<_
key,fr
edit,Éditer
close,Fermer
_

File.write('test',content)

file = CSV.read('test', :headers => true)
file.headers # => ["key", "fr"]
file.to_a # => [["key", "fr"], ["edit", "Éditer"], ["close", "Fermer"]]

当您设置:headers => true 是CSV数据的第一行,您将会看到标题。考虑到这个定义,上面的内容很好。但下面一个矛盾:

When you set :headers => true, the first row of the CSV data, you have will be tread as header. With this definition in mind, The above goes well. But the below one contradicts :

require 'csv'

File.write('test',content)

file = CSV.read('test', :headers => true)
file.headers # => ["key", "fr"]
file.to_a # => [["key", "fr"], ["edit", "Éditer"], ["close", "Fermer"]]

content = <<_
key,fr

edit,Éditer
close,Fermer
_

File.write('test1',content)

file = CSV.read('test1', :headers => true)
file.headers # => []  # <~~~~~~~~~~~ Is this a bug ?
file.to_a # => [[], [], ["edit", "Éditer"], ["close", "Fermer"]]


b $ b

file.headers 给出一个空数组 [] ,你可以看到, strong> 0 th 输出 file.to_a 的索引也是一个空数组( [] )。但它似乎是一个错误。因此,我提出了

file.headers gives an empty array [], as you can see that in the 0th index of the output of file.to_a is also an empty array([]). But it seems to a bug. Thus I raised the issue.

这篇关于如果第一行后跟一个空行,Ruby CSV不会找到文件头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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