2008-10-30 20 views
4

我有一個CSV數據文件,其中的行可能有很多列500+,有些則很少。我需要轉置它,以便每行成爲輸出文件中的一列。的問題是,在原始文件中的行可以不都具有相同的列數,所以當我嘗試陣列的轉置方法獲得:使用FasterCSV將不均勻的行轉換爲列

`轉置':元件尺寸不同(12應爲5) (IndexError)

是否有替代轉置與數組長度不一致?

+1

人,那是甜蜜的是紅寶石已轉置內置陣列。我通常會寫一個腳本來用其他語言來完成它。 – wprl 2008-10-30 16:25:39

回答

9

我會插入空值來填充你的矩陣的孔中,一些諸如:

a = [[1, 2, 3], [3, 4]] 

# This would throw the error you're talking about 
# a.transpose 

# Largest row 
size = a.max { |r1, r2| r1.size <=> r2.size }.size 

# Enlarge matrix inserting nils as needed 
a.each { |r| r[size - 1] ||= nil } 

# So now a == [[1, 2, 3], [3, 4, nil]] 
aa = a.transpose 

# aa == [[1, 3], [2, 4], [3, nil]] 
+0

結束使用此解決方案。謝謝 – srboisvert 2008-11-02 12:44:09

2
# Intitial CSV table data 
csv_data = [ [1,2,3,4,5], [10,20,30,40], [100,200] ] 

# Finding max length of rows 
row_length = csv_data.map(&:length).max 

# Inserting nil to the end of each row 
csv_data.map do |row| 
    (row_length - row.length).times { row.insert(-1, nil) } 
end 

# Let's check 
csv_data 
# => [[1, 2, 3, 4, 5], [10, 20, 30, 40, nil], [100, 200, nil, nil, nil]] 

# Transposing... 
transposed_csv_data = csv_data.transpose 

# Hooray! 
# => [[1, 10, 100], [2, 20, 200], [3, 30, nil], [4, 40, nil], [5, nil, nil]]