简体   繁体   English

读取文件并验证Ruby中的行

[英]Read file and validate rows in Ruby

I have a CSV file as following 我有一个CSV文件,如下所示

ID      Required  -- these are headers
SD0005   Yes      -- row information

I have to validate each row against header. 我必须针对标题验证每一行。 Say ID contains letters and numbers and length should not be more than 6. Required header should be either yes or no in every row. 说出ID包含字母和数字,且长度不得超过6。要求的标头在每一行中应为yes或no。

How can I achieve this functionality in Ruby if I have to process a huge file which has more than 1000 rows with good performance? 如果必须处理性能超过1000行的巨大文件,如何在Ruby中实现此功能?

I'm reading particular row against each header as follows 我正在读取每个标题的特定行,如下所示

CSV.foreach('file path', :headers => true) do |csv_obj| 
csv_obj['ID'] 
csv_obj['Required']

Is there a way to know which condition failed while validating column against header for a row. 有没有办法知道在针对行的标题验证列时哪个条件失败。 I need to know for which condition it failed and print it out 我需要知道失败的原因并打印出来

New to ruby. 红宝石的新手。 Appreciate help 感谢帮助

To get the data into Ruby from the CSV file, try the following: 要从CSV文件将数据导入Ruby,请尝试以下操作:

# This will read the data in with headers, convert the column names to symbols, 
# and then turn each resulting CSV::Row instance into a hash

data = CSV.read('temp.csv', headers: true, header_converters: :symbol).map(&:to_h)

this should return the following: 这应该返回以下内容:

=> [{:id=>"SD0005", :required=>" yes"}, ...]

Once you have all of your information in a format you can work with in Ruby, you could make a method that checks the validity of each ID. 一旦拥有了可以在Ruby中使用的格式的所有信息,就可以创建一种检查每个ID有效性的方法。

def valid_id?(id_string)
  # uses Regular Expressions to ensure ID is 6 
  # characters that consist of only numbers/letters
  # The double-bang(!!) turn a truthy value to `true`
  # or false value to `false`

  !!id_string.match(/^[\D|\d]{6}$/)
end

If you wanted to test another column's validity, do so with a separate method. 如果要测试另一列的有效性,请使用单独的方法进行。

def valid_required?(req_column)
  req_column.downcase == 'yes' ||   req_column.downcase == 'no'
end

Create a main validator method 创建一个主验证器方法

def all_valid?(row)
  valid_id?(row[:id]) && valid_required?(row[:required])
end

Then keep only the records whose ID is valid 然后仅保留其ID有效的记录

# #select keeps values whose block evaluates to `true`
valid_records = data.select { |record| all_valid?(record) }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM