[英]How to remove repeated columns using ruby FasterCSV
我正在使用Ruby 1.8和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
假設您要擺脫硬編碼的刪除
row.delete(6) #delete second acct_num field
row.delete(4) #delete second acct_id field
可以替換為
row = row.to_hash
這將破壞重復項。 其余已發布的代碼將繼續工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.