我在Ruby中學習CSV函數,雖然我可以成功將數組寫入csv文件,但我無法將該文件轉換回數組。測試代碼如下(我的應用程序只需要在數組中的整數)Ruby Reading CSV問題
require 'rubygems'
requires 'csv'
array = [1,2,3,4,5,6,7,8]
CSV.open('array.csv', 'w') do |csv|
csv << array
puts array.inspect
new_array = Array.new
new_array = CSV.read('array.csv', converters: :numeric)
puts new_array.inspect
end
這將返回
[1, 2, 3, 4, 5, 6, 7, 8]
[]
的array.csv文件寫入和填充(1,2,3,4,5, 6,7,8)但是當我讀它時,我只是返回一個空數組。