[英]ruby How I could print without leave newline space for each line?
ruby How I could print without leave newline space for each line 如何在不为每行保留换行符的情况下进行打印
this is my file 这是我的档案
name1;name2;name3
name4;name5;name6
I have this command 我有这个命令
File.open("text2xycff.txt", "r") do |fix|
fix.readlines.each do |line|
parts = line.chomp.split(';')
input3= zero
File.open('text2xyczzzz2.txt', 'a+') do |f|
f.puts parts[0] , parts[1], parts[2] ,input3
end
end
end
this is my output 这是我的输出
name1
name2
name3
zero
name4
name5
name6
zero
I need to output this 我需要输出这个
name1;name2;name3;zero
name4;name5;name6;zero
Please help me whit this problem 请帮我解决这个问题
A more minimal approach is to just append something to each line: 一种更简单的方法是在每行后面添加一些内容:
File.open("text2xycff.txt", "r") do |input|
File.open('text2xyczzzz2.txt', 'a+') do |output|
input.readlines.each do |line|
output.puts(line.chomp + ';zero')
end
end
end
Or if you want to actually parse things, which presents an opportunity for clean-up: 或者,如果您想实际解析事物,则提供了清理的机会:
File.open("text2xycff.txt", "r") do |input|
File.open('text2xyczzzz2.txt', 'a+') do |output|
input.readlines.each do |line|
parts = line.chomp.split(/;/)
parts << 'zero'
output.puts(parts.join(';'))
end
end
end
If you call puts
with comma-separated arguments, each one of them will be printed on a different line. 如果您使用逗号分隔的参数调用puts
,则每个参数将打印在不同的行上。
You can use ruby string interpolation here ( http://ruby-for-beginners.rubymonstas.org/bonus/string_interpolation.html ): 您可以在此处使用红宝石字符串插值( http://ruby-for-beginners.rubymonstas.org/bonus/string_interpolation.html ):
f.puts "#{parts[0]};#{parts[1]};#{parts[3]};#{input3}"
You have two solutions. 您有两种解决方案。
The first one uses puts
as you currently do: 第一个使用puts
与当前操作相同:
File.open('yourfile.txt', 'a+') { |f|
f.puts "#{parts[0]}#{parts[1]}#{parts[2]}#{input3}"
}
The second one uses write
instead of puts
: 第二个使用write
而不是puts
:
File.open('yourfile.txt', 'a+') { |f|
f.write parts[0]
f.write parts[1]
f.write parts[2]
f.write input3
}
Try: 尝试:
File.open("test_io.txt", "r") do |fix|
fix.readlines.each do |line|
File.open('new_file10.txt', 'a+') do |f|
next if line == "\n"
f.puts "#{line.chomp};zero"
end
end
end
I'm not sure why you're splitting the string by semicolon when you specified you wanted the below output. 我不确定为什么要在指定以下输出时用分号分隔字符串。 You would be better served just appending ";zero" to the end of the string rather than parsing an array. 仅将“; zero”附加到字符串的末尾而不是解析数组会更好。
name1;name2;name3;zero 名称1;名称2;名称3;零
name4;name5;name6;zero 名称4;名称5;名称6;零
You can specify an if
statement to check for the zero
value. 您可以指定一个if
语句来检查zero
值。
Example: 例:
arr = ["name1", "name2", "name3", "zero", "name4", "name5", "name6", "zero"];
arr.each { |x|
if x != "zero" then
print x
else
puts x
end
}
Output: 输出:
name1name2name3zero
name4name5name6zero
print
will print inline. print
将内联打印。
puts
will print on a new line. puts
将在新行中打印。
Just implement this logic in your code and you're good to go. 只需在您的代码中实现此逻辑,就可以了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.