如何在 Ruby 中读取文件的行
我试图使用以下代码从文件中读取行。但是当读取文件时,内容都在一行中:
line_num=0
File.open('xxx.txt').each do |line|
print "#{line_num += 1} #{line}"
end
但是这个文件 分别打印每一行。
我必须使用标准输入,例如 ruby my_prog.rb ruby my_prog.rb
ruby my_prog.rb
ruby my_prog.rb < file.txt
,我无法假设文件使用的行结束字符是什么。我该如何处理?
I was trying to use the following code to read lines from a file. But when reading a file, the contents are all in one line:
line_num=0
File.open('xxx.txt').each do |line|
print "#{line_num += 1} #{line}"
end
But this file prints each line separately.
I have to use stdin, like ruby my_prog.rb < file.txt
, where I can't assume what the line-ending character is that the file uses. How can I handle it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
Ruby 确实有一个方法:
http://ruby -doc.org/core-1.9.3/IO.html#method-c-readlines
Ruby does have a method for this:
http://ruby-doc.org/core-1.9.3/IO.html#method-c-readlines
这将为文件中的每一行执行给定的块,而不会将整个文件放入内存中。请参阅:IO::foreach。
This will execute the given block for each line in the file without slurping the entire file into memory. See: IO::foreach.
我相信我的答案涵盖了您对处理任何类型的行结尾的新担忧,因为
"\r\n"
和"\r"
都转换为 Linux 标准在解析行之前使用“\n”
。要支持
"\r"
EOL 字符以及 Windows 中的常规"\n"
和"\r\n"
,如下我会做什么:当然,对于非常大的文件来说,这可能是一个坏主意,因为这意味着将整个文件加载到内存中。
I believe my answer covers your new concerns about handling any type of line endings since both
"\r\n"
and"\r"
are converted to Linux standard"\n"
before parsing the lines.To support the
"\r"
EOL character along with the regular"\n"
, and"\r\n"
from Windows, here's what I would do:Of course this could be a bad idea on very large files since it means loading the whole file into memory.
您的第一个文件具有 Mac Classic 行结尾(即
"\r"
而不是通常的"\n"
)。打开它以指定行结尾。
Your first file has Mac Classic line endings (that’s
"\r"
instead of the usual"\n"
). Open it withto specify the line endings.
对于具有标题的文件,我偏向于以下方法:
这允许您以与内容行不同的方式处理标题行(或多行)。
I'm partial to the following approach for files that have headers:
This allows you to process a header line (or lines) differently than the content lines.
这是因为每行都有结束线。
使用 ruby 中的 chomp 方法删除末尾的结束行 '\n' 或 'r'。
It is because of the endlines in each lines.
Use the chomp method in ruby to delete the endline '\n' or 'r' at the end.
gets 怎么样?
how about gets ?
不要忘记,如果您担心读取的文件中可能包含大量行,从而在运行时会淹没您的 RAM,那么您始终可以分段读取该文件。请参阅“为什么读取文件不好”。
Don't forget that if you are concerned about reading in a file that might have huge lines that could swamp your RAM during runtime, you can always read the file piece-meal. See "Why slurping a file is bad".
已经有无数的答案,但这里是为那些喜欢使用路径名的人提供的一个。
There's already a bazillion answers, but here is one for those who like to use Pathnames.