If I have an array of strings e.g.
如果我有一个字符串数组。
a = ['a', 'b', 'c', 'd']
and I want to output the elements, to a file (e.g. .txt) one per line. So far I have:
我想把元素输出到一个文件(例如。txt)每行一个。到目前为止我有:
File.new("test.txt", "w+")
File.open("test.txt", "w+") do |i|
i.write(a)
end
This gives me the array on one line of the test.txt file. How can I iterate over the array, adding each value to a new line of the file?
这给出了测试一行上的数组。txt文件。如何对数组进行迭代,向文件的新行添加每个值?
5 个解决方案
#1
75
Either use Array#each
to iterate over your array and call IO#puts
to write each element to the file (puts
adds a record separator, typically a newline character):
或者使用数组#each遍历数组并调用IO#put将每个元素写入文件(放入一个记录分隔符,通常是一个换行字符):
File.open("test.txt", "w+") do |f|
a.each { |element| f.puts(element) }
end
Or pass the whole array to puts
:
或将整个数组传递给put:
File.open("test.txt", "w+") do |f|
f.puts(a)
end
From the documentation:
从文档:
If called with an array argument, writes each element on a new line.
如果使用数组参数调用,则在新行上写入每个元素。
#2
8
There is a quite simpler solution :
有一个非常简单的解决方案:
IO.write("file_name.txt", your_array.join("\n"))
#3
5
As an alternate, you could simply join the array with "\n" so that each element is on a new line, like this:
作为替代,您可以使用“\n”来连接数组,以便每个元素都位于一条新线上,如下所示:
a = %w(a b c d)
File.open('test.txt', 'w') {|f| f.write a.join("\n")}
If you don't want to override the values already in the text file so that you're simply adding new information to the bottom, you can do this:
如果您不希望覆盖文本文件中已有的值,以便在底部添加新信息,您可以这样做:
a = %w(a b c d)
File.open('test.txt', 'a') {|f| f << "\n#{a.join("\n")}"}
#4
2
Use Array#each
to iterate each element. When writing to the file, make sure you append newline(\n
), or you will get a file with abcd
as content:
使用数组#each迭代每个元素。在写入文件时,确保添加了newline(\n),否则将会得到一个包含abcd的文件,内容如下:
a = ['a', 'b', 'c', 'd']
File.open('test.txt', 'w') do |f|
a.each do |ch|
f.write("#{ch}\n")
end
end
#5
2
Another simple solution:
另一个简单的解决方案:
directory = "#{Rails.root}/public/your_directory" #create your_directory before
file_name = "your_file.txt"
path = File.join(directory, file_name)
File.open(path, "wb") { |f| f.write(your_array.join("\n")) }
#1
75
Either use Array#each
to iterate over your array and call IO#puts
to write each element to the file (puts
adds a record separator, typically a newline character):
或者使用数组#each遍历数组并调用IO#put将每个元素写入文件(放入一个记录分隔符,通常是一个换行字符):
File.open("test.txt", "w+") do |f|
a.each { |element| f.puts(element) }
end
Or pass the whole array to puts
:
或将整个数组传递给put:
File.open("test.txt", "w+") do |f|
f.puts(a)
end
From the documentation:
从文档:
If called with an array argument, writes each element on a new line.
如果使用数组参数调用,则在新行上写入每个元素。
#2
8
There is a quite simpler solution :
有一个非常简单的解决方案:
IO.write("file_name.txt", your_array.join("\n"))
#3
5
As an alternate, you could simply join the array with "\n" so that each element is on a new line, like this:
作为替代,您可以使用“\n”来连接数组,以便每个元素都位于一条新线上,如下所示:
a = %w(a b c d)
File.open('test.txt', 'w') {|f| f.write a.join("\n")}
If you don't want to override the values already in the text file so that you're simply adding new information to the bottom, you can do this:
如果您不希望覆盖文本文件中已有的值,以便在底部添加新信息,您可以这样做:
a = %w(a b c d)
File.open('test.txt', 'a') {|f| f << "\n#{a.join("\n")}"}
#4
2
Use Array#each
to iterate each element. When writing to the file, make sure you append newline(\n
), or you will get a file with abcd
as content:
使用数组#each迭代每个元素。在写入文件时,确保添加了newline(\n),否则将会得到一个包含abcd的文件,内容如下:
a = ['a', 'b', 'c', 'd']
File.open('test.txt', 'w') do |f|
a.each do |ch|
f.write("#{ch}\n")
end
end
#5
2
Another simple solution:
另一个简单的解决方案:
directory = "#{Rails.root}/public/your_directory" #create your_directory before
file_name = "your_file.txt"
path = File.join(directory, file_name)
File.open(path, "wb") { |f| f.write(your_array.join("\n")) }