gsub help

K

Kvetch Kvetch

I am reading in a file that is basically a csv file but the delimiter is
spaces. I am also using fastercsv to parse the file into an array.
Before fastercsv reads it I am substituting all spaces into commas with
gsub
l.gsub!(" ", ",")
I have one column in each row that could contain spaces. For example
firstname,lastname,phonenumber,streetname,zip
john crichton 555-555-1234 ^A street name with spaces^ 12345
turns into
john,crichton,555-555-1234,A,street,name,with,spaces,12345
I can place the column that could contain spaces at the end of each row
and then call like this but I would like to preserve those spaces.
FasterCSV.parse(l) do |row|
name=row[1]
lastname=row[2]
phone=row[3]
zip=row[4]
street=row[6...10]
Each street name starts with a ^ and ends with a ^. Does anyone know if
there is way I can replace all spaces up until the first ^ and then
begin again after the last ^ so the street name is preserved with spaces
and each column is separated by commas so my result set looks like the
following?
john,crichton,555-555-1234,A street name with spaces,12345

Thanks
 
R

Robert Gleeson

Hi,

This isn't particularly efficient or clever, but it should work:

"john crichton 555-555-1234 ^A street name with spaces^
12345".scan(/(\w+) (\w+) (\S+) \^(.+)\^ (\d+)/).join ','

- Rob
 
Y

yermej

I am reading in a file that is basically a csv file but the delimiter is
spaces.  I am also using fastercsv to parse the file into an array.

Check out the docs for fastercsv - http://fastercsv.rubyforge.org/ -
it looks like the FasterCSV class has default values for separator and
quote characters. You should be able to change those to ' ' & '^',
respectively.
 
S

Siep Korteling

yermej said:
Check out the docs for fastercsv - http://fastercsv.rubyforge.org/ -
it looks like the FasterCSV class has default values for separator and
quote characters. You should be able to change those to ' ' & '^',
respectively.

Exactly. Like this:

ar=FasterCSV.read( path_to_file,{ :col_sep => " ", :quote_char =>'^' })

hth,

Siep
 
K

Kvetch Kvetch

yermej said:
Check out the docs for fastercsv - http://fastercsv.rubyforge.org/ -
it looks like the FasterCSV class has default values for separator and
quote characters. You should be able to change those to ' ' & '^',
respectively.

Thanks yermej. I wasn't using the fastercsv separator because I was
performing some actions on the data before I read it in with fastercsv.
In the end it probably doesn't matter but thank you for your help.
 
K

Kvetch Kvetch

Siep said:
Exactly. Like this:

ar=FasterCSV.read( path_to_file,{ :col_sep => " ", :quote_char =>'^' })

hth,

Siep

Thanks Siep. I got it to work nicely. I appreciate you and Yernej's
help
 
K

Kvetch Kvetch

yermej said:
Check out the docs for fastercsv - http://fastercsv.rubyforge.org/ -
it looks like the FasterCSV class has default values for separator and
quote characters. You should be able to change those to ' ' & '^',
respectively.

Thanks Yermej. I got the quote characters to solve my problem.
Appreciate it.
 
R

Robert Klemme

2009/12/13 Siep Korteling said:
Exactly. Like this:

ar=3DFasterCSV.read( path_to_file,{ :col_sep =3D> " ", :quote_char =3D>'^=
' })

You can omit brackets to make it look a little better:

ar =3D FasterCSV.read( path_to_file, :col_sep =3D> " ", :quote_char =3D>'^'=
)

or, maybe a bit more efficient because you read line by line:

CSV.foreach(path_to_file, :col_sep =3D> " ") do |row|
...
end

Note, in 1.9.* FasterCSV has replaced CSV which is why you can use CSV ther=
e.

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top