How do i ignore rows with unique string while parsing a csv file in ruby
i开发者_如何学C want to ignore ALL
TopLevel, Result,,,,,,,,
AddHost,10.1.3.1,,,,,,,,
and
Flush,,,,,,,,,
AddHost,10.1.3.4,,,,,,,,
from
TopLevel, Result,,,,,,,,
AddHost,10.1.3.1,,,,,,,,
Add,10.1.3.1,43172
Add,10.1.3.1,44172
Add,10.1.3.1,4172
Add,10.1.3.1,432
Add,10.1.3.1,435472
Flush,,,,,,,,,
AddHost,10.1.3.4,,,,,,,,
Given the contents of the CSV file:
s = "TopLevel, Result,,,,,,,,
AddHost,10.1.3.1,,,,,,,,
Add,10.1.3.1,43172
Add,10.1.3.1,44172
Add,10.1.3.1,4172
Add,10.1.3.1,432
Add,10.1.3.1,435472
Flush,,,,,,,,,
AddHost,10.1.3.4,,,,,,,,
"
gsub will remove the offending lines:
s = s.gsub(/TopLevel, Result,,,,,,,,\nAddHost,10.1.3.1,,,,,,,,\n/, '')
s = s.gsub(/Flush,,,,,,,,,\nAddHost,10.1.3.4,,,,,,,,\n/, '')
puts s
# => Add,10.1.3.1,43172
# => Add,10.1.3.1,44172
# => Add,10.1.3.1,4172
# => Add,10.1.3.1,432
# => Add,10.1.3.1,435472
You may want regular expressions less specific than that. For example, if you want to remove ANY AddHost line following a TopLevel line, then the first regexp would be:
/TopLevel, Result,,,,,,,,\nAddHost.*\n/
When gsub is done munching on the file, pass the results to the CSV parser as normal.
精彩评论