I'm working on a method to take a CSV file (with headers) and parse it using Ruby CSV.parse, however I only want to save specific columns.
The CSV looks like this:
NAME,SUPERNET_IP,POP_NAME,ADDRESS_BLOCK_START,ADDRESS_BLOCK_END,Service,ISP Service ID,WCC,DUNSID
Retail,186.43.168.0,text1,186.43.168.0,186.43.175.255,XYZ,XYZB00090095,Enabled,227015716
Retail,186.57.80.0,text2,186.57.80.0,186.57.87.255,XYZ,XYXB00090095,Enabled,227015716
and the only fields I want to keep are:
POP_NAME,ADDRESS_BLOCK_START,ADDRESS_BLOCK_END,WCC
Is there a way to parse in the CSV by specific header names, like:
mycsv = CSV.parse(csv_data, {:headers => true, (list of headers to keep here) })
This example is assuming csv_data is a string formed from the example CSV above.
As a stopgap I'm just converting the CSV into an array of arrays but it's not really what I'm after. I'd rather keep it as a CSV object.
myreturnedcsv = []
mycsv = CSV.parse(csv_data, {:headers => true, })
mycsv.each do |row|
myreturnedcsv.push([row[2], row[3], row[4],row[7]])
end