【问题标题】:Write an array to multi column CSV format using Ruby使用 Ruby 将数组写入多列 CSV 格式
【发布时间】:2017-06-18 16:16:34
【问题描述】:

我在 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.

谁能指出我的“写”方向来获得:

(i) .text 文件,可以像这样放置我的数据。

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,它不会将其输出到文件中。它以我想要的方式在我的控制台中输出它,但不是在我将它写入文件时。

感谢您花时间阅读这一切。

【问题讨论】:

    标签: ruby export-to-csv file-writing


    【解决方案1】:

    感谢@cameck 和@tkupari,这两个答案都是我想要的。最后选择了 Cameck 的答案,因为它“剪切”了剪切和粘贴文本 => 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) }
    

    【讨论】:

      【解决方案2】:
      File.open('02.3test.gyro_trends.text' , 'w') { |file| trend_array.each { |a| file.puts(a.join(","))}}
      

      【讨论】:

      • 可恶,我知道我错过了这么简单的东西。
      • @RyanAnderson 请注意,ruby 的标准库中有一个 CSV 库,它将为您提供此功能。 See CSV#add_row 了解更多详情。 Writing Header 将为您提供有关如何实现此功能的提示
      • 感谢工程师mnky,每个人都非常乐于助人。喜欢堆栈!
      【解决方案3】:

      交替使用CSV Class:

        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
      

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

      【讨论】:

      • 卡梅克,你就是男人!非常感谢。正是我想要的!
      • 太棒了!乐于助人:)
      猜你喜欢
      • 1970-01-01
      • 2022-09-23
      • 1970-01-01
      • 1970-01-01
      • 2015-08-07
      • 2018-06-14
      • 2015-04-28
      • 2019-04-09
      • 2014-02-23
      相关资源
      最近更新 更多