在ruby中解析文本文件

时间:2022-09-06 00:21:34

My text file looks like this

我的文本文件看起来像这样

 VOTE 1168041805 Campaign:ssss_uk_01B Validity:during Choice:Antony CONN:MIG01TU MSISDN:00777778359999 GUID:E6109CA1-7756-45DC-8EE7-677CA7C3D7F3 Shortcode:63334
 VOTE 1168041837 Campaign:ssss_uk_01B Validity:during Choice:Leon CONN:MIG00VU MSISDN:00777770939999 GUID:88B52A7B-A182-405C-9AE6-36FCF2E47294 Shortcode:63334

I want to get value of vote campaign validity choice for which I am doing this:

我希望获得投票活动有效性选择的价值,我这样做:

     File.foreach('lib/data/file.txt') do |line|
       line = line.tidy_bytes
       begin
         aline = line.match(/^VOTE\s(\d+)\sCampaign:([^ ]+)\sValidity:([^ ]+)\sChoice:([^ ]+)/)
         unless aline.nil?
             ## do something
         end
       rescue Exception => e
        raise " error: " + e.inspect
        p line.inspect
        next
       end
     end

Is there any better way for doing this for

有没有更好的方法来做到这一点

      aline = line.match(/^VOTE\s(\d+)\sCampaign:([^ ]+)\sValidity:([^ ]+)\sChoice:([^ ]+)/)

and getting aline[1] aline[2] aline[3] and aline[4]

aline [1] aline [2] aline [3]和aline [4]

1 个解决方案

#1


1  

You can use named captures to get a hash of results instead:

您可以使用命名捕获来获取结果的哈希值:

# use a freezed contant instead of making a new Regexp object for each line 
REGEXP = /^VOTE\s(?<id>\d+)\sCampaign:(?<campaign>[^ ]+)\sValidity:(?<validity>[^ ]+)\sChoice:(?<choice>[^ ]+)/.freeze

File.foreach('lib/data/file.txt') do |line|
   begin
      matches = line.tidy_bytes.match(REGEXP)
      hash = matches.names.zip(matches.captures).to_h 
   end
   rescue Exception => e
     raise " error: " + e.inspect
     p line.inspect
     next
   end
 end

If the desired result is an array you might want to use .map:

如果所需的结果是数组,则可能需要使用.map:

# use a freezed contant instead of making a new Regexp object for each line 
REGEXP = /^VOTE\s(?<id>\d+)\sCampaign:(?<campaign>[^ ]+)\sValidity:(?<validity>[^ ]+)\sChoice:(?<choice>[^ ]+)/.freeze

results = File.foreach('lib/data/file.txt').map do |line|
   matches = line.tidy_bytes.match(REGEXP)
   matches.names.zip(matches.captures).to_h 
 end

#1


1  

You can use named captures to get a hash of results instead:

您可以使用命名捕获来获取结果的哈希值:

# use a freezed contant instead of making a new Regexp object for each line 
REGEXP = /^VOTE\s(?<id>\d+)\sCampaign:(?<campaign>[^ ]+)\sValidity:(?<validity>[^ ]+)\sChoice:(?<choice>[^ ]+)/.freeze

File.foreach('lib/data/file.txt') do |line|
   begin
      matches = line.tidy_bytes.match(REGEXP)
      hash = matches.names.zip(matches.captures).to_h 
   end
   rescue Exception => e
     raise " error: " + e.inspect
     p line.inspect
     next
   end
 end

If the desired result is an array you might want to use .map:

如果所需的结果是数组,则可能需要使用.map:

# use a freezed contant instead of making a new Regexp object for each line 
REGEXP = /^VOTE\s(?<id>\d+)\sCampaign:(?<campaign>[^ ]+)\sValidity:(?<validity>[^ ]+)\sChoice:(?<choice>[^ ]+)/.freeze

results = File.foreach('lib/data/file.txt').map do |line|
   matches = line.tidy_bytes.match(REGEXP)
   matches.names.zip(matches.captures).to_h 
 end