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

R

Raimon Fs

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 => ?;) 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
 
T

Tiziano Merzi

Raimon said:
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 => ?;) do |row|

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

CSV.open(arxiu.file_path , "r", ?;) do |row|

Tiziano
 
R

Raimon Fs

James said:
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
 

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,777
Messages
2,569,604
Members
45,234
Latest member
SkyeWeems

Latest Threads

Top