Parsing a csv using ; => ruby doesn't split the row

Hello,

I'm importing CSV files without problems, now, I have some of them that
use the semi-colon instead of the comma as a field separator, I'm trying
to adapt my Ruby code for that, but Ruby puts all the columns in the
first element, without using the new value as a separator:

here's the code:

聽聽聽CSV.open(arxiu.file_path , "r", :col_sep => ?:wink: do |row|

聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽@data_new=Cat.new
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽@data_new.reporter = row[0]
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽@data_new.partner = row[1]
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽@data_new.product = row[2]
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽@data_new.period = row[3]
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽@data_new.imp = row[4]
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽@data_new.exp = row[5]
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽@data_new.pes_imp = row[6]
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽@data_new.pes_exp = row[7]

聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽@data_new.save

聽聽聽聽聽聽聽聽聽聽聽聽聽end # CSV open do

and here is the file:

Catalunya;UE;TARIC;ANY;IMP;EXP;PES IMP;PES EXP
Catalunya;UE;01021010;2006;6205,1;195,7;0,0;0,0
Catalunya;UE;01021030;2006;66,9;147,7;0,0;0,0
Catalunya;UE;01021090;2006;614,8;3317,2;0,0;0,0
Catalunya;UE;01029005;2006;79166,1;4603,0;0,0;0,0
Catalunya;UE;01029029;2006;19572,2;259,0;5820,0;0,0
.......

and this is what I get:
@data_new.reporter=Catalunya;UE;TARIC;ANY;IMP;EXP;PES IMP;PES EXP
@data_new.partner = ""
@data_new.product = ""
@data_new.period = ""
@data_new.imp = ""
@data_new.exp = ""
@data_new.pes_imp = ""
@data_new.pes_exp = ""

and this is what I want:

@data_new.reporter=Catalunya
@data_new.partner = UE
@data_new.product = TARIC
@data_new.period = ANY
@data_new.imp = IMP
@data_new.exp = EXP
@data_new.pes_imp = PES IMP
@data_new.pes_exp = PES EXP

...

thanks !

raimon

路路路

--
Posted via http://www.ruby-forum.com/.

Raimon Fs wrote:

Hello,

I'm importing CSV files without problems, now, I have some of them that
use the semi-colon instead of the comma as a field separator, I'm trying
to adapt my Ruby code for that, but Ruby puts all the columns in the
first element, without using the new value as a separator:

here's the code:

   CSV.open(arxiu.file_path , "r", :col_sep => ?:wink: do |row|

col_sep is an optional argument, the call to open is:

     CSV.open(arxiu.file_path , "r", ?:wink: do |row|

Tiziano

路路路

--
Posted via http://www.ruby-forum.com/\.

   CSV.open(arxiu.file_path , "r", :col_sep => ?:wink: do |row|

col_sep is an optional argument, the call to open is:

     CSV.open(arxiu.file_path , "r", ?:wink: do |row|

Tiziano

thanks, it works now !

regards,

raimon

路路路

--
Posted via http://www.ruby-forum.com/\.

Just to be clear though, what you had originally is the new syntax used in Ruby 1.9.

James Edward Gray II

路路路

On Jan 5, 2008, at 4:30 AM, Raimon Fs wrote:

  CSV.open(arxiu.file_path , "r", :col_sep => ?:wink: do |row|

col_sep is an optional argument, the call to open is:

    CSV.open(arxiu.file_path , "r", ?:wink: do |row|

Tiziano

thanks, it works now !

James Gray wrote:

路路路

On Jan 5, 2008, at 4:30 AM, Raimon Fs wrote:

thanks, it works now !

Just to be clear though, what you had originally is the new syntax
used in Ruby 1.9.

James Edward Gray II

ok, thanks again, I was using that syntax from some docs, and I didn't
understand why it wasn't working, I'm using 1.8.x Ruby with Rails ...

raimon
--
Posted via http://www.ruby-forum.com/\.