Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Ruby将数组写入多列CSV格式_Ruby_Export To Csv_File Writing - Fatal编程技术网

使用Ruby将数组写入多列CSV格式

使用Ruby将数组写入多列CSV格式,ruby,export-to-csv,file-writing,Ruby,Export To Csv,File Writing,我有一个Ruby中的数组,我正试图将其输出到CSV文件或文本。然后,我可以轻松地将其传输到另一个XML文件进行绘图 我似乎不能像这样得到文本格式的输出。相反,我得到的是一行数据,它只是一个大数组 0,2 0,3 0,4 0,5 我最初尝试过类似的方法 File.open('02.3.gyro_trends.text' , 'w') { |file| trend_array.each { |x,y| file.puts(x,y)}} 它输出 0.2 46558 0 46560 0 ....et

我有一个Ruby中的数组,我正试图将其输出到CSV文件或文本。然后,我可以轻松地将其传输到另一个XML文件进行绘图

我似乎不能像这样得到文本格式的输出。相反,我得到的是一行数据,它只是一个大数组

0,2
0,3
0,4
0,5
我最初尝试过类似的方法

File.open('02.3.gyro_trends.text' , 'w') { |file| trend_array.each { |x,y| file.puts(x,y)}}
它输出

0.2
46558
0
46560
0
....etc etc.
有人能给我指点写作方向吗

文本文件,可以像这样放置我的数据

trend_array[0][0], trend_array[0][1] 
trend_array[1][0], trend_array[1][1] 
trend_array[2][0], trend_array[2][1] 
trend_array[3][0], trend_array[3][1] 
ii.csv文件,该文件将把这些数据放在单独的列中。 编辑我最近在数组中添加了两个以上的值,结合Cameck的解决方案查看我的答案

这就是我目前拥有的

trend_array=[]
j=1

# cycle through array and find change in gyro data.

while j < gyro_array.length-2

  if gyro_array[j+1][1] < 0.025 && gyro_array[j+1][1] > -0.025
    trend_array << [0, gyro_array[j][0]]
    j+=1
  elsif gyro_array[j+1][1] > -0.025  # if the next value is      increasing by x1.2 the value of the previous amount. Log it as +1
    trend_array << [0.2, gyro_array[j][0]]
    j+=1
  elsif gyro_array[j+1][1] <  0.025 # if the next value is   decreasing by x1.2 the value of the previous amount. Log it as -1
    trend_array << [-0.2, gyro_array[j][0]]
    j+=1
  end
end

#for graphing and analysis purposes (wanted to print it all as a csv  in two columns)

File.open('02.3test.gyro_trends.text' , 'w') { |file| trend_array.each { |x,y| file.puts(x,y)}}

File.open('02.3test.gyro_trends_count.text' , 'w') { |file| trend_array.each {|x,y| file.puts(y)}}
我知道这很容易,但不知为什么我错过了。我发现,如果我尝试在最后一行代码中连接\\n,它不会将其输出到文件中。它以我想要的方式在我的控制台中输出,但不是在我将其写入文件时输出

感谢您抽出时间阅读这些内容

交替使用:

File.open('02.3test.gyro_trends.text' , 'w') { |file| trend_array.each { |a| file.puts(a.join(","))}}

用数组[col_1,col_2,col_3]调用write_to_csv

谢谢@cameck和@tkupari,这两个答案都是我想要的。最终还是同意了Cameck的答案,因为它省去了剪切和粘贴text=>xml。下面是我如何将一组数组放入它们的适当位置

require 'csv'

CSV_HEADER = [
                "Apples",
                "Oranges",
                "Pears"
                        ]

@csv_name = "Test_file.csv"

def write_to_csv(row)
  if csv_exists?
    CSV.open(@csv_name, 'a+') { |csv| csv << row }
  else
    # create and add headers if doesn't exist already
    CSV.open(@csv_name, 'wb') do |csv|
      csv << CSV_HEADER
      csv << row
    end
  end
end

def csv_exists?
  @exists ||= File.file?(@csv_name)
end

array = [ [1,2,3] , ['a','b','c'] , ['dog', 'cat' , 'poop'] ]
array.each { |row| write_to_csv(row) }

邪恶的,我知道我错过了这么简单的东西。@RyanAnderson需要注意的是,ruby的标准库中有一个CSV库,可以为您提供此功能。更多细节。我会给你一些关于如何实现这个的提示。谢谢工程师们,每个人都很有帮助。爱这堆!卡梅克,你就是那个人!谢谢。正是我要找的!令人惊叹的很乐意帮忙:
require 'csv'

CSV_HEADER = [
                "Apples",
                "Oranges",
                "Pears"
                        ]

@csv_name = "Test_file.csv"

def write_to_csv(row)
  if csv_exists?
    CSV.open(@csv_name, 'a+') { |csv| csv << row }
  else
    # create and add headers if doesn't exist already
    CSV.open(@csv_name, 'wb') do |csv|
      csv << CSV_HEADER
      csv << row
    end
  end
end

def csv_exists?
  @exists ||= File.file?(@csv_name)
end

array = [ [1,2,3] , ['a','b','c'] , ['dog', 'cat' , 'poop'] ]
array.each { |row| write_to_csv(row) }