我是ruby编程的新手。 我试图逐行读取文本文件。
这是我的示例文本文件:
john doe john_d somepassword这是我的代码:
f = File.open('input.txt', 'r') a = f.readlines n = a[0] s = a[1] u = a[2] p = a[3] str = "<user><name=\"#{n}\" surname=\"#{s}\" username=\"#{u}\" password=\"#{p}\"/></user>" File.open('result.txt', 'w') { |file| file.print(str) }输出应如下所示:
<user><name="john" surname="doe" username="john_d" password="somepassword"/></user>但是result.txt看起来像这样。 它包括每行的换行符:
<user><name="john " surname="doe " username="john_d " password="somepassword"/></user>我怎么能纠正这个?
I am new in ruby programming. I am trying to read a textfile line by line.
Here is my sample textfile:
john doe john_d somepasswordHere is my code:
f = File.open('input.txt', 'r') a = f.readlines n = a[0] s = a[1] u = a[2] p = a[3] str = "<user><name=\"#{n}\" surname=\"#{s}\" username=\"#{u}\" password=\"#{p}\"/></user>" File.open('result.txt', 'w') { |file| file.print(str) }The output should look like this:
<user><name="john" surname="doe" username="john_d" password="somepassword"/></user>But the result.txt looks like this. It includes newline character for every line:
<user><name="john " surname="doe " username="john_d " password="somepassword"/></user>How can i correct this?
最满意答案
它包括每行的换行符,因为每行末尾都有换行符。
只是在你不需要它时删除它:
n = a[0].gsub("\n", '') s = a[1].gsub("\n", '') # ...It includes newline character for every line, because there is a newline character at the end of every line.
Just removed it when you don't need it:
n = a[0].gsub("\n", '') s = a[1].gsub("\n", '') # ...如何用RUBY读取文本文件中的行(How can i read lines in a textfile with RUBY)我是ruby编程的新手。 我试图逐行读取文本文件。
这是我的示例文本文件:
john doe john_d somepassword这是我的代码:
f = File.open('input.txt', 'r') a = f.readlines n = a[0] s = a[1] u = a[2] p = a[3] str = "<user><name=\"#{n}\" surname=\"#{s}\" username=\"#{u}\" password=\"#{p}\"/></user>" File.open('result.txt', 'w') { |file| file.print(str) }输出应如下所示:
<user><name="john" surname="doe" username="john_d" password="somepassword"/></user>但是result.txt看起来像这样。 它包括每行的换行符:
<user><name="john " surname="doe " username="john_d " password="somepassword"/></user>我怎么能纠正这个?
I am new in ruby programming. I am trying to read a textfile line by line.
Here is my sample textfile:
john doe john_d somepasswordHere is my code:
f = File.open('input.txt', 'r') a = f.readlines n = a[0] s = a[1] u = a[2] p = a[3] str = "<user><name=\"#{n}\" surname=\"#{s}\" username=\"#{u}\" password=\"#{p}\"/></user>" File.open('result.txt', 'w') { |file| file.print(str) }The output should look like this:
<user><name="john" surname="doe" username="john_d" password="somepassword"/></user>But the result.txt looks like this. It includes newline character for every line:
<user><name="john " surname="doe " username="john_d " password="somepassword"/></user>How can i correct this?
最满意答案
它包括每行的换行符,因为每行末尾都有换行符。
只是在你不需要它时删除它:
n = a[0].gsub("\n", '') s = a[1].gsub("\n", '') # ...It includes newline character for every line, because there is a newline character at the end of every line.
Just removed it when you don't need it:
n = a[0].gsub("\n", '') s = a[1].gsub("\n", '') # ...
发布评论