我如何处理Ruby中的大文件? [英] How can I handle large files in Ruby?
问题描述
编辑:这是我的代码:
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屋!