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

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", ?;) do |row|

Tiziano

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", ?;) do |row|

Tiziano

thanks, it works now !

regards,

raimon

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 G. II

James G. 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 G. 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