【问题标题】:ruby gedcom parser EOF exceptionruby gedcom 解析器 EOF 异常
【发布时间】:2014-05-04 09:51:11
【问题描述】:

我需要为分析项目解析 gedcom 5.5 文件。 我发现的第一个 ruby​​ 解析器会导致堆栈级别太深的错误,所以我试图找到替代方案。我找到了这个项目:https://github.com/jslade/gedcom-ruby

包含了一些示例,但我也没有让它们工作。

这是解析器本身:https://github.com/jslade/gedcom-ruby/blob/master/lib/gedcom.rb

如果我尝试这样的示例:

ruby ./samples/count.rb ./samples/royal.ged

我收到以下错误:

D:/rails_projects/gedom_test/lib/gedcom.rb:185:in `readchar': end of file reached (EOFError)

为了更好地理解,我在每个方法中都写了一个“gets”,这是异常引发之前的输出:

Parsing './samples/royal.ged'...
INIT
BEFORE
CHECK_PROC_OR_BLOCK
BEFORE
CHECK_PROC_OR_BLOCK
PARSE
PARSE_FILE
PARSE_IO
DETECT_RS

导致问题的确切行是

while ch = io.readchar

在detect_rs方法中:

# valid gedcom may use either of \r or \r\n as the record separator.
# just in case, also detects simple \n as the separator as well
# detects the rs for this string by scanning ahead to the first occurence
# of either \r or \n, and checking the character after it
def detect_rs io
puts "DETECT_RS"
  rs = "\x0d"
  mark = io.pos
  begin
    while ch = io.readchar
      case ch
      when 0x0d
        ch2 = io.readchar
        if ch2 == 0x0a
          rs = "\x0d\x0a"
        end
        break
      when 0x0a
        rs = "\x0a"
        break
      end
    end
  ensure
    io.pos = mark
  end
  rs
end

我希望有人可以帮助我。

【问题讨论】:

    标签: ruby parsing gedcom


    【解决方案1】:

    Ruby 的IO 类的readchar 方法在遇到文件末尾时会引发EOFErrorhttp://www.ruby-doc.org/core-2.1.1/IO.html#method-i-readchar

    gedcom-ruby gem 已经好几年没被人碰过了,但它的一个分支花了几年时间来解决这个问题。

    基本上它会改变:

    while ch = io.readchar
    

    while !io.eof && ch = io.readchar
    

    您可以在这里获得 gem 的分叉:https://github.com/trentlarson/gedcom-ruby

    【讨论】:

    • 谢谢,这解决了异常问题,但它现在也不起作用。它说文件不包含任何人,这是不正确的:'samples/royal.ged'中有0个人和0个家庭。
    • 抱歉,我不知道 gedcom 文件 - 我只是想让您摆脱 EOF 错误。但是只是为了看看,我在我的答案中从 fork 下载了 gedcom,当我运行 ruby samples/count.rb samples/royal.ged 时,它给了我 There are 3010 individuals and 1422 families in 'samples/royal.ged'.
    • 是的..我在 Windows 机器上使用 ruby​​ 和 rails,我在虚拟 linux 机器上尝试了相同的确切示例并获得正确的输出。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-16
    • 2012-10-19
    • 1970-01-01
    • 2012-05-02
    • 1970-01-01
    • 1970-01-01
    • 2018-04-05
    相关资源
    最近更新 更多