【发布时间】:2014-10-19 17:54:10
【问题描述】:
我对 Ruby 的 CSV 模块做了一些工作,但在让它忽略多个标题行时遇到了一些问题。
具体来说,这里是我要解析的文件的前二十行:
USGS Digital Spectral Library splib06a
Clark and others 2007, USGS, Data Series 231.
For further information on spectrsocopy, see: http://speclab.cr.usgs.gov
ASCII Spectral Data file contents:
line 15 title
line 16 history
line 17 to end: 3-columns of data:
wavelength reflectance standard deviation
(standard deviation of 0.000000 means not measured)
( -1.23e34 indicates a deleted number)
----------------------------------------------------
Olivine GDS70.a Fo89 165um W1R1Bb AREF
copy of splib05a r 5038
0.205100 -1.23e34 0.090781
0.213100 -1.23e34 0.018820
0.221100 -1.23e34 0.005416
0.229100 -1.23e34 0.002928
第 10 行给出实际的标题,第 17 行是实际数据的开始位置。
这是我的代码:
require "nyaplot"
# Note that DataFrame basically just inherits from Ruby's CSV module.
class SpectraHelper < Nyaplot::DataFrame
class << self
def from_csv filename
df = super(filename, col_sep: ' ') do |csv|
csv.convert do |field, info|
STDERR.puts "Field is #{field}"
end
end
end
end
def csv_headers
[:wavelength, :reflectance, :standard_deviation]
end
end
def read_asc filename
f = File.open(filename, "r")
16.times do
line = f.gets
puts "Ignoring #{line}"
end
d = SpectraHelper.from_csv(f)
end
输出表明我对f.gets 的调用实际上并没有忽略这些行,我不明白为什么。以下是输出的前几行:
Field is Clark
Field is and
Field is others
Field is 2007,
Field is USGS,
我尝试寻找显示处理更复杂 CSV 文件的教程或示例,但运气不佳。如果有人可以向我指出一个可以回答这个问题的资源,我将不胜感激(并且更愿意将其标记为已接受,而不是针对我的特定问题的解决方案——但两者都将不胜感激)。
使用 Ruby 2.1。
【问题讨论】: