Ruby 1.9.3 - 如果CSV文件中没有标题,CSV.table如何知道?

时间:2021-10-08 11:20:08

I have been doing some testing with CSV.table. I have two small and almost identical CSV files, however one is missing the header row.

我一直在用CSV.table做一些测试。我有两个小而几乎相同的CSV文件,但是有一个缺少标题行。

When I run CSV.table against the CSV file with the header row, everything works as expected.

当我使用标题行对CSV文件运行CSV.table时,一切都按预期工作。

When I run it against the CSV file without the header row I get:

当我针对没有标题行的CSV文件运行它时,我得到:

NoMethodError: undefined method `encode' for nil:NilClass

I tried this with different types of data, with different types of headers, and get the same results.

我尝试使用不同类型的数据,使用不同类型的标头,并获得相同的结果。

I am curious to the magic of CSV.table. If I use CSV.parse with headers set to true, then it always makes the first row be headers no matter what. So, I have been using CSV.table to check if a CSV file being imported has a header row but I am not too comfortable with this because I don't understand if or when it will or will not work the way I'm using it.

我很好奇CSV.table的魔力。如果我使用标题设置为true的CSV.parse,那么它总是使第一行成为标题,无论如何。所以,我一直在使用CSV.table来检查导入的CSV文件是否有标题行,但我对此不太满意,因为我不明白它是否或何时会以我正在使用的方式工作它。

begin
  CSV.table(csv_file_path)
rescue
  # Add error to log or something.
end

Does anyone know?

有人知道吗?

P.S. I've already read through this and the source code it provides on each method - http://www.ruby-doc.org/stdlib-1.9.3/libdoc/csv/rdoc/CSV.html

附:我已经阅读了它以及它为每种方法提供的源代码 - http://www.ruby-doc.org/stdlib-1.9.3/libdoc/csv/rdoc/CSV.html

1 个解决方案

#1


9  

There isn't any magic involved, and it won't work for you in general.

没有任何魔法,一般来说它不适合你。

As you can see from the source, table literally just calls read with headers: true. But it also converts the header to symbols (header_converters: :symbol) and this is the key to why it appears to work.

正如您从源代码中看到的那样,表格实际上只是使用headers调用read:true。但它也会将标题转换为符号(header_converters :: symbol),这就是它看起来有效的关键。

You get an error without headers because you have a blank column in your first row of data (something like a,b,,d,e). The blank gets read in as nil, and since nil can't be converted to a symbol, it blows up.

没有标题会出现错误,因为第一行数据中有一个空白列(类似a,b ,, d,e)。空白被读入为零,并且由于无法将nil转换为符号,因此它会爆炸。

Try it with some data that doesn't have a blank in the first row - you'll see that table will treat that row of data as headers just like any of the other methods.

尝试使用第一行中没有空白的一些数据 - 您将看到该表将该行数据视为标题,就像任何其他方法一样。

#1


9  

There isn't any magic involved, and it won't work for you in general.

没有任何魔法,一般来说它不适合你。

As you can see from the source, table literally just calls read with headers: true. But it also converts the header to symbols (header_converters: :symbol) and this is the key to why it appears to work.

正如您从源代码中看到的那样,表格实际上只是使用headers调用read:true。但它也会将标题转换为符号(header_converters :: symbol),这就是它看起来有效的关键。

You get an error without headers because you have a blank column in your first row of data (something like a,b,,d,e). The blank gets read in as nil, and since nil can't be converted to a symbol, it blows up.

没有标题会出现错误,因为第一行数据中有一个空白列(类似a,b ,, d,e)。空白被读入为零,并且由于无法将nil转换为符号,因此它会爆炸。

Try it with some data that doesn't have a blank in the first row - you'll see that table will treat that row of data as headers just like any of the other methods.

尝试使用第一行中没有空白的一些数据 - 您将看到该表将该行数据视为标题,就像任何其他方法一样。