我正在使用Ruby 1.8和FasterCSV。如何使用紅寶石刪除重複的列FasterCSV
我讀的csv文件有幾個重複的列。
| acct_id | amount | acct_num | color | acct_id | acct_type | acct_num |
| 345 | 12.34 | 123 | red | 345 | 'savings' | 123 |
| 678 | 11.34 | 432 | green | 678 | 'savings' | 432 |
...等
我想濃縮成:
| acct_id | amount | acct_num | color | acct_type |
| 345 | 12.34 | 123 | red | 'savings' |
| 678 | 11.34 | 432 | green | 'savings' |
有沒有一種通用的方式來做到這一點?
目前我的解決辦法是這樣的:
headers = CSV.read_line(file)
headers = CSV.read_line # get rid of garbage line between headers and data
FasterCSV.filter(file, :headers => headers) do |row|
row.delete(6) #delete second acct_num field
row.delete(4) #delete second acct_id field
# additional processing on the data
row['color'] = color_to_number(row['color'])
row['acct_type'] = acct_type_to_number(row['acct_type'])
end
是你有沒有工作? – 2011-04-05 19:23:00
它可以工作,但並不高雅。例如。我有另一個不同索引表的類似問題。 – mkirk 2011-04-05 20:11:45