0

我有一个像这样的纯文本表。我需要对结果行进行分组,以便数据在各自的列中一起出现。

我可以在一个空格上拆分字符串(一行),然后我会得到一个数组,如:

["2", "1/47", "M4044", "25:03*", "856", "12:22", "12:41", "17.52", "Some", "Name", "Yo", "Prairie", "Inn", "Harriers", "Runni", "25:03"]

我也可以分成两个空格,这让我很接近,但仍然不一致,正如您在名称中看到的那样:

["2", " 1/47", "M4044", " 25:03*", "856", " 12:22", " 12:41", "17.52 Some Name Yo", "", "", "", "", "", "", "Prairie Inn Harriers Runni", " 25:03 "]

我可以指定要加入的索引,但我可能需要像这样抓取数千个文件,并且列并不总是以相同的顺序排列。

一个常量是列数据永远不会长于列名和数据之间的分隔符(the ====)。我试图利用这一点来发挥自己的优势,但发现了一些漏洞。

我需要编写一个算法来检测名称列中保留的内容以及其他“单词”列中保留的内容。有什么想法吗?

4

4 回答 4

4

首先我们设置问题:

data = <<EOF
Place Div/Tot Div   Guntime  PerF 1sthalf 2ndhalf 100m   Name                      Club                       Nettime 
===== ======= ===== =======  ==== ======= ======= ====== ========================= ========================== ======= 
    1   1/24  M3034   24:46   866   12:11   12:35  15.88 Andy Bas                  Prairie Inn Harriers         24:46 
    2   1/47  M4044   25:03*  856   12:22   12:41  17.52 Some Name Yo              Prairie Inn Harriers Runni   25:03 
EOF
lines = data.split "\n"

我喜欢为 String#unpack 创建一个格式字符串:

format = lines[1].scan(/(=+)(\s+)/).map{|f, s| "A#{f.size}" + 'x' * s.size}.join
#=> A5xA7xA5xA7xxA4xA7xA7xA6xA25xA26xA7x

剩下的很简单:

headers = lines[0].unpack format
lines[2..-1].each do |line|
  puts Hash[headers.zip line.unpack(format).map(&:strip)]
end
#=> {"Place"=>"1", "Div/Tot"=>"1/24", "Div"=>"M3034", "Guntime"=>"24:46", "PerF"=>"866", "1sthalf"=>"12:11", "2ndhalf"=>"12:35", "100m"=>"15.88", "Name"=>"Andy Bas", "Club"=>"Prairie Inn Harriers", "Nettime"=>"24:46"}
#=> {"Place"=>"2", "Div/Tot"=>"1/47", "Div"=>"M4044", "Guntime"=>"25:03", "PerF"=>"856", "1sthalf"=>"12:22", "2ndhalf"=>"12:41", "100m"=>"17.52", "Name"=>"Some Name Yo", "Club"=>"Prairie Inn Harriers Runni", "Nettime"=>"25:03"}
于 2012-10-11T06:54:06.990 回答
2

这应该工作

divider = "===== ======= ===== =======  ==== ======= ======= ====== ========================= ========================== ======="
str     = "    1   1/24  M3034   24:46   866   12:11   12:35  15.88 Andy Bas                  Prairie Inn Harriers         24:46"

divider.split(/\s+/).each {|delimiter| puts str.slice!(0..delimiter.size).strip }
于 2012-10-11T06:24:41.443 回答
0

这是一个可行的解决方案(基于您给定的文件-但我猜应该推广到这种形式的所有文件):

#!/usr/bin/env ruby

FILE = 'gistfile1.txt'

f = File.new(FILE,'r')

l = f.gets #read first line (which contains the headers)

#parse for the columns: header text, where they start,stop & len
headers = l.scan(/(\S+\W+)/).each.map{|s| [s.join, l.index(s.join), s.join.length]}.map{|a| {:head=>a[0].strip,:start=>a[1],:end=>a[1]+a[2],:len=>a[2]}}

f.gets #to skip past the header-data separator line

records = []

while( l = f.gets)
    record = {}
    headers.each{|h|
        record[h[:head]] = l[h[:start]...h[:end]].strip
        print "#{h[:head]} => #{record[h[:head]]}\n"
    }   
    print "*" * l.length,"\n"
    records << record
end

#records contains each record, with each column header mapped onto the respective data record

对于演示,我在进行过程中回显记录:

Place => 1
Div/Tot => 1/24
Div => M3034
Guntime => 24:46
PerF => 866
1sthalf => 12:11
2ndhalf => 12:35
100m => 15.88
Name => Andy Bas
Club => Prairie Inn Harriers
Nettime => 24:46
***********************************************************************************************************************
Place => 2
Div/Tot => 1/47
Div => M4044
Guntime => 25:03*
PerF => 856
1sthalf => 12:22
2ndhalf => 12:41
100m => 17.52
Name => Some Name Yo
Club => Prairie Inn Harriers Runni
Nettime => 25:03
**********************************************************************************************************************
于 2012-10-11T07:06:15.757 回答
0
header, format, *data = plain_text_table.split($/)
h = {}
format.scan(/=+/) do
  range = $~.begin(0)..$~.end(0)
  h[header[range].strip] = data.map{|s| s[range].strip}
end

h # => {
  "Place" => ["1", "2"],
  "Div/Tot" => ["1/24", "1/47"],
  "Div" => ["M3034", "M4044"],
  "Guntime" => ["24:46", "25:03*"],
  "PerF" => ["866", "856"],
  "1sthalf" => ["12:11", "12:22"],
  "2ndhalf" => ["12:35", "12:41"],
  "100m" => ["15.88", "17.52"],
  "Name" => ["Andy Bas", "Some Name Yo"],
  "Club" => ["Prairie Inn Harriers", "Prairie Inn Harriers Runni"],
  "Nettime" => ["24:46", "25:03"]
}
于 2012-10-11T09:28:07.700 回答