如何将我的文件设置为不提取 csv 文件中的标题?我已经尝试过这个:
CSV.foreach(email_response_file, :col_sep => "\t", :return_headers => false) do |column|
....
end
Run Code Online (Sandbox Code Playgroud)
但是,无论我设置:return_headers
为 true 还是 false,我仍然会拉入标题。我怎样才能摆脱它们?我认为我的问题是.foreach
我正在使用的方法。
:return_headers
:headers
仅在是的情况下才有效true
,但它不会按照您想象的方式工作。
如果您的 CSV 数据包含标题行,只需设置headers: true
,第一行不会作为数据行返回。相反,它被解析并允许您通过其标头访问字段(如哈希):
require 'csv'
data=<<-eos
id,name
1,foo
2,bar
eos
CSV.parse(data, headers: true) do |row|
puts "ID: " + row["id"]
puts "Name: " + row["name"]
puts "1st col: " + row[0]
puts "2nd col: " + row[1]
puts "---"
end
Run Code Online (Sandbox Code Playgroud)
输出:
ID: 1
Name: foo
1st col: 1
2nd col: foo
---
ID: 2
Name: bar
1st col: 2
2nd col: bar
---
Run Code Online (Sandbox Code Playgroud)