Question

I have a csv file which has a few areas where / proceeded by nothing and proceeded by nothing that I'd like to strip out. The catch is, I also have other items in the file which have a / that I don't want to strip out.

an example:

/abc, /, akaksdhfaiwe
/, /foo, /bar

I'd like to be:

/abc,, akaksdhfaiwe
, /foo, /bar

How do I do this? I can't use gsub('^/', '') because it would strip out the /abc /foo /bar. And for the life of me, I can't seem to find an 'ends with'. I was hoping that \Z would work, no luck.

Any takers?

I've been using fasterCSV for a lot of the manipulation which has been pretty great so far.

Was it helpful?

Solution

If your file isn't that big then you could slurp it into memory while cleaning it up and then write it out again:

clean = [ ]
CSV.open(your_csv).each { |r| csv << r.map { |e| e.sub(/^\s*\/$/, '') } }
out = CSV.open(your_csv, 'wb')
clean.each { |r| out << r }  
out.close

If you somehow have an incredibly massive CSV file that won't fit in memory all at once:

out = CSV.open('tempfile.csv', 'wb')
CSV.open(your_csv).each { |r| out << r.map { |e| e.sub(/^\s*\/$/, '') } }
out.close
File.rename('tempfile.csv', your_csv)

Either should turn this:

/abc, /, akaksdhfaiwe
/, /foo, /bar

into

/abc,"", akaksdhfaiwe
"", /foo, /bar

OTHER TIPS

With ruby you can do:

"/asdf /, /fdsa".gsub("/,", ",")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top