For some reason, I can't find any tutorial mentioning how to do this... So, how do I read the first n lines from a file?
出于某种原因,我找不到任何提及如何执行此操作的教程...那么,如何从文件中读取前n行?
I've come up with:
我想出来了:
while File.open('file.txt') and count <= 3 do |f|
...
count += 1
end
end
but it is not working and it also doesn't look very nice to me.
但它不起作用,对我来说也不是很好看。
Just out of curiosity, I've tried things like:
出于好奇,我尝试过这样的事情:
File.open('file.txt').10.times do |f|
but that didn't really work either.
但那也没有用。
So, is there a simple way to read just the first n lines without having to load the whole file? Thank you very much!
那么,有没有一种简单的方法来读取前n行而不必加载整个文件?非常感谢你!
5 个解决方案
#1
22
Here is a one-line solution:
这是一个单行解决方案:
lines = File.foreach('file.txt').first(10)
I was worried that it might not close the file in a prompt manner (it might only close the file after the garbage collector deletes the Enumerator returned by File.foreach). However, I used strace
and I found out that if you call File.foreach
without a block, it returns an enumerator, and each time you call the first
method on that enumerator it will open up the file, read as much as it needs, and then close the file. That's nice, because it means you can use the line of code above and Ruby will not keep the file open any longer than it needs to.
我担心它可能不会以提示方式关闭文件(它可能只在垃圾收集器删除File.foreach返回的Enumerator后关闭文件)。但是,我使用了strace,我发现如果你在没有块的情况下调用File.foreach,它会返回一个枚举器,每次调用该枚举器上的第一个方法时,它将打开文件,尽可能多地读取,然后关闭该文件。这很好,因为这意味着你可以使用上面的代码行,Ruby不会让文件保持打开状态超过它需要的时间。
#2
5
There are many ways you can approach this problem in Ruby. Here's one way:
有很多方法可以在Ruby中解决这个问题。这是一种方式:
File.open('Gemfile') do |f|
lines = 10.times.map { f.readline }
end
#3
3
File.foreach('file.txt').with_index do |line, i|
break if i >= 10
puts line
end
#4
0
File inherits from IO and IO mixes in Enumerable methods which include #first
文件继承自包含#first的Enumerable方法中的IO和IO混合
Passing an integer to first(n)
will return the first n items in the enumberable collection. For a File object, each item is a line in the file.
将整数传递给first(n)将返回enumberable集合中的前n个项。对于File对象,每个项目都是文件中的一行。
File.open('filename.txt', 'r').first(10)
This returns an array of the lines including the \n line breaks. You may want to #join
them to create a single whole string.
这将返回包含\ n换行符的行数组。您可能希望#join它们来创建一个完整的字符串。
File.open('filename.txt', 'r').first(10).join
#5
-1
You could try the following:
您可以尝试以下方法:
`head -n 10 file`.split
It's not really "pure ruby" but that's rarely a requirement these days.
它不是真正的“纯红宝石”,但这些日子很少需要。
#1
22
Here is a one-line solution:
这是一个单行解决方案:
lines = File.foreach('file.txt').first(10)
I was worried that it might not close the file in a prompt manner (it might only close the file after the garbage collector deletes the Enumerator returned by File.foreach). However, I used strace
and I found out that if you call File.foreach
without a block, it returns an enumerator, and each time you call the first
method on that enumerator it will open up the file, read as much as it needs, and then close the file. That's nice, because it means you can use the line of code above and Ruby will not keep the file open any longer than it needs to.
我担心它可能不会以提示方式关闭文件(它可能只在垃圾收集器删除File.foreach返回的Enumerator后关闭文件)。但是,我使用了strace,我发现如果你在没有块的情况下调用File.foreach,它会返回一个枚举器,每次调用该枚举器上的第一个方法时,它将打开文件,尽可能多地读取,然后关闭该文件。这很好,因为这意味着你可以使用上面的代码行,Ruby不会让文件保持打开状态超过它需要的时间。
#2
5
There are many ways you can approach this problem in Ruby. Here's one way:
有很多方法可以在Ruby中解决这个问题。这是一种方式:
File.open('Gemfile') do |f|
lines = 10.times.map { f.readline }
end
#3
3
File.foreach('file.txt').with_index do |line, i|
break if i >= 10
puts line
end
#4
0
File inherits from IO and IO mixes in Enumerable methods which include #first
文件继承自包含#first的Enumerable方法中的IO和IO混合
Passing an integer to first(n)
will return the first n items in the enumberable collection. For a File object, each item is a line in the file.
将整数传递给first(n)将返回enumberable集合中的前n个项。对于File对象,每个项目都是文件中的一行。
File.open('filename.txt', 'r').first(10)
This returns an array of the lines including the \n line breaks. You may want to #join
them to create a single whole string.
这将返回包含\ n换行符的行数组。您可能希望#join它们来创建一个完整的字符串。
File.open('filename.txt', 'r').first(10).join
#5
-1
You could try the following:
您可以尝试以下方法:
`head -n 10 file`.split
It's not really "pure ruby" but that's rarely a requirement these days.
它不是真正的“纯红宝石”,但这些日子很少需要。