Stripping double quotes

Hi,

I have the attached file, but my issue is that the file I am parsing, is
in this format:

“45hc43j”,“sdfsdf”,"",“Dead”,“sdfdsf “,“sgfsdf”,””,“sdfg”,“null”,"",

That is why I escape quotes in the script. How do I get an output that
doesn’t have double quotes? I tried using gsub but it was of no use.

Also, the data snippet above is a csv file…but if you were to output
it to command line and text after running the script, it returns those
elements as quotes as seen above.

Any help will be appreciated.

Mick

On Tue, Jan 10, 2012 at 07:31:11AM +0900, Mickey V. wrote:

Hi,

I have the attached file, but my issue is that the file I am parsing, is
in this format:

“45hc43j”,“sdfsdf”,"",“Dead”,“sdfdsf “,“sgfsdf”,””,“sdfg”,“null”,"",

That is why I escape quotes in the script. How do I get an output that
doesn’t have double quotes? I tried using gsub but it was of no use.

How is gsub no use?

$ irb
>> string = '"45hc43j","sdfsdf","","Dead","sdfdsf 

“,“sgfsdf”,”",“sdfg”,“null”,"",’
=> ““45hc43j”,“sdfsdf”,”",“Dead”,“sdfdsf
“,“sgfsdf”,””,“sdfg”,“null”,"","
>> new_string = string.gsub(’"’, ‘’)
=> “45hc43j,sdfsdf,Dead,sdfdsf ,sgfsdf,sdfg,null,”

That seems to work just fine for me.

On Mon, Jan 9, 2012 at 11:31 PM, Mickey V. [email protected]
wrote:

Also, the data snippet above is a csv file…but if you were to output
it to command line and text after running the script, it returns those
elements as quotes as seen above.

Any help will be appreciated.

Since you are really trying to open a CSV file … use
the existing CSV library

http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV.html

There are other cases that you might miss (e.g. newlines
inside a “cell” are allowed (e.g. a return inside a cell in
Excell/OpenOffice is possible) and may trip you). Probably
there are more corner cases that are non-trivial in the
beginning.

HTH,

Peter

Chad P. wrote in post #1040132:

On Tue, Jan 10, 2012 at 07:31:11AM +0900, Mickey V. wrote:

How do I get an output that doesn’t have double quotes? I tried
using gsub but it was of no use.

How is gsub no use?

Because String#tr() is a better choice. :wink:

new_string = string.gsub(’"’, ‘’)

new_string = string.tr(’"’, ‘’)

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs