开发者

Parsing text using Ruby

开发者 https://www.devze.com 2023-01-17 14:58 出处:网络
I have the following text which will always follow the same format: 1 \"13\" \"241\" \"Rabun\" \"06\"

I have the following text which will always follow the same format:

 1
 "13"
 "241"
 "Rabun"
 "06"
 "County"

 2
 "13"
 "281"
 "Towns"
 "06"
 "County"

I would like to assign each section to a hash like:

locality= {:id => "", :fips1 => "", :fips2 => "", :county => "", :stateid => "", :type => ""}

How would I go about doing this in Ruby? Any help i开发者_开发问答s greatly appreciated.


fields = [:fips1,:fips2,:county,:stateid,:type]
arraywithhashes = yourtextdata.split("\n\n").map { |loc|
    Hash[
        [[:id,loc[/\d+/]]] +
        fields.zip(loc.scan(/"([^"]+)"/).map &:first)
    ]
}

If you add new fields to your file, the only you'll need to edit is to add it to fields.


for each section, use a regular expression with groups corresponding to each entry in the section, then simply create hash table as you described from these groups.


locality.each_key { |k| locality.store(k, "foo") }

Another newbie-ish person here, but that might be a start for you.


You might want to consider using a Struct instead of a Hash.

Locality = Struct.new(:id, :fips1, :fips2, :county, :stateid, :type)
localities = []
DATA.each_slice(7) do |chunk|
    chunk.pop if chunk.size == 7 
    localities << Locality.new(*chunk.map{|line| line.scan(/\w+/) })
end

p localities # => [#<struct Locality id=["1"], fips1=["13"], fips2=["241"], etc. 
puts localities[1].fips2 # => 281

__END__
1
 "13"
 "241"
 "Rabun"
 "06"
 "County"

 2
 "13"
 "281"
 "Towns"
 "06"
 "County"
  • each_slice(7) takes 7 lines of DATA (the stuff after __END__ ).

  • The last line is removed unless there are only six lines (the last 'record').

  • A cleaned-up copy of the remaining lines is made. With these values a new Locality is created and added to an array

0

精彩评论

暂无评论...
验证码 换一张
取 消