简体   繁体   English

Ruby文件在最后一个空行之后无法读取内容\\ n

[英]Ruby File gets not reading content after last blank line \n

I'm trying to write a very simple ruby script that opens a text file, removes the \\n from the end of lines UNLESS the line starts with a non-alphabetic character OR the line itself is blank (\\n). 我正在尝试编写一个非常简单的ruby脚本,该脚本会打开一个文本文件,从行尾删除\\ n,除非该行以非字母字符开头或者该行本身为空白(\\ n)。

The code below works fine, except that it skips all of the content beyond the last \\n line. 下面的代码工作正常,除了跳过最后\\ n行之外的所有内容。 When I add \\n\\n to the end of the file, it works perfectly. 当我在文件末尾添加\\ n \\ n时,它可以正常工作。 Examples: A file with this text in it works great and pulls everything to one line: 示例:包含此文本的文件效果很好,并将所有内容拖到一行:

Hello
there my
friend how are you?

becomes Hello there my friend how are you? Hello there my friend how are you?

But text like this: 但是这样的文字:

Hello

there

my friend
how
are you today

returns just Hello and There , and completely skips the last 3 lines. 仅返回HelloThere ,并完全跳过最后三行。 If I add 2 blank lines to the end, it will pick up everything and behave as I want it to. 如果我在末尾添加2个空行,它将拾取所有内容并按我希望的方式运行。

Can anybody explain to me why this happens? 有人可以向我解释为什么会这样吗? Obviously I know I can fix this instance by appending \\n\\n to the end of the source file at the start, but that doesn't help me understand why the .gets isn't working as I'd expect. 显然,我知道可以通过在开始时在源文件的末尾附加\\n\\n来修复此实例,但这并不能帮助我理解为什么.gets无法正常工作。

Thanks in advance for any help! 在此先感谢您的帮助!

source_file_name = "somefile.txt"
destination_file_name = "some_other_file.txt"
source_file = File.new(source_file_name, "r")

para = []
x = ""
while (line = source_file.gets)
  if line != "\n"
    if line[0].match(/[A-z]/)   #If the first character is a letter
        x += line.chomp + " "
    else
      x += "\n" + line.chomp + " "
    end
  else
    para[para.length] = x
    x = ""
  end
end

source_file.close

fixed_file = File.open(destination_file_name, "w")
para.each do |paragraph|
  fixed_file << "#{paragraph}\n\n"
end
fixed_file.close

Your problem lies in the fact you only add your string x to the para array if and only if you encounter an empty line ('\\n'). 您的问题在于,只有在遇到空行('\\ n')时,才将字符串x添加到para数组。 Since your second example does not contain the empty line at the end, the final contents of x are never added to the para array. 由于第二个示例的末尾不包含空行,因此x的最终内容永远不会添加到para数组中。

The easy way to fix this without changing any of your code, is add the following lines after closing your while loop: 在不更改任何代码的情况下解决此问题的简单方法是在关闭while循环后添加以下几行:

if(x != "")
    para.push(x)
end

I would prefer to add the strings to my array right away rather then appending them onto x until you hit an empty line, but this should work with your solution. 我宁愿立即将字符串添加到我的数组中,而不是将它们附加到x上直到您碰到空行,但这应该与您的解决方案一起使用。

Also, 也,

para.push(x)
para << x

both read much nicer and look more straightforward than 两者都比阅读的要好得多,看起来也比

para[para.length] = x

That one threw me off for a second, since in non-dynamic languages, that would give you an error. 那把我甩了一秒钟,因为在非动态语言中,这会给你带来错误。 I advise using one of those instead, simply because it's more readable. 我建议改用其中之一,因为它更具可读性。

您的代码对我来说就像是ac代码, ruby的方式应该是这样,它可以替代您上面的100行。

File.write "dest.txt", File.read("src.txt")

It's easier to use a multiline regex. 使用多行正则表达式会更容易。 Maybe: 也许:

source_file.read.gsub(/(?<!\n)\n([a-z])/im, ' \\1')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM