我如何处理Ruby中的大文件? [英] How can I handle large files in Ruby?

查看:171
本文介绍了我如何处理Ruby中的大文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编程很新,所以要温柔一点。我试图从库数据库.dat文件中提取IBSN号码。我已经编写了可以工作的代码,但是它只能搜索大约180MB文件的一半。我怎样才能调整它来搜索整个文件?或者如何编写一个程序将数据文件拆分成可管理的块?



编辑:这是我的代码:

  export = File.new(resultsfinal.txt,w +)

File.open(bibrec2。 dat)。每个do | line |
line.scan(/ [a] {1} [1234567890xX] {10} \W /)do | x |
export.puts x
end
line.scan(/ [a] {1} [1234567890xX] {13} /)do | x |
export.puts x
end
end


解决方案



只要你知道我已经做了一个脚本有点儿相同的语法来搜索真正的〜8GB的大文件没有问题。

$ $ p $ export = File.new(resultsfinal。 txt,w +)

File.open(bibrec2.dat)。
begin
line.scan(/ [a] {1} [1234567890xX] {10} \W /)do | x |
export.puts x
end
line.scan(/ [a] {1} [1234567890xX] {13} /)do | x |
export.puts x
end
rescue
puts添加结果时出现问题
结束
结束


I'm pretty new to programming, so be gentle. I'm trying to extract IBSN numbers from a library database .dat file. I have written code that works, but it is only searching through about half of the 180MB file. How can I adjust it to search the whole file? Or how can I write a program the will split the dat file into manageable chunks?

edit: Here's my code:

export = File.new("resultsfinal.txt","w+")

File.open("bibrec2.dat").each do |line|
  line.scan(/[a]{1}[1234567890xX]{10}\W/) do |x|
    export.puts x
  end
  line.scan(/[a]{1}[1234567890xX]{13}/) do |x|
    export.puts x
  end
end

解决方案

You should try to catch exception to check if the problem is really on the read block or not.

Just so you know I already made a script with kinda the same syntax to search real big file of ~8GB without problem.

export = File.new("resultsfinal.txt","w+")

File.open("bibrec2.dat").each do |line|
  begin
    line.scan(/[a]{1}[1234567890xX]{10}\W/) do |x|
      export.puts x
    end
    line.scan(/[a]{1}[1234567890xX]{13}/) do |x|
      export.puts x
    end
  rescue
    puts "Problem while adding the result"
  end
end

这篇关于我如何处理Ruby中的大文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆