目前我有一个类似于:
的csv文件 1%, 2%, 3%, 4%,
1m, $1.00, $1.50, $2.75, $3.00
2m, $1.25, $1.10, $2.00, $3.50
我正在尝试检查每个排列的适当金额。大多数情况下,我遇到一个问题,找到一个角度来解决这个问题。我的草率代码如下所示。我99%肯定我会以错误的方式解决这个问题。
CSV.foreach("some.csv") do |row|
@row = row.to_s
if @row.include?('%')
percent_char = []
percent_char.push(@row)
end
if @row.include?('m')
m_char = []
m_char.push(@row)
end
end
m_char.each do |m|
percent_char.each do |percent|
puts "Increment m_char #{m} and increment #{percent}"
#per increment I want to be able to assert the proper dollar amount
#this will assert true no matter what, but would just like to learn how to get here
assert_equals(dollar, dollar)
end
end
我在这里有一个“美元”的占位符,主要是为了说明我想要实现的目标。可能有人有任何想法我应该怎么做?
答案 0 :(得分:0)
也许你正在寻找这样的东西?
CSV.foreach('some.csv', headers: true) do |row|
m_char = nil
row.each do |percent, dollar|
if m_char
# whatever kind of validation you wanted goes here
else
m_char = dollar
end
end
end
但这只有在您修复CSV文件时才有效。您在第一行中缺少值,因此百分比不对齐。你需要一个额外的逗号:
, 1%, 2%, 3%, 4%
1m, $1.00, $1.50, $2.75, $3.00
2m, $1.25, $1.10, $2.00, $3.50