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

**URL:** https://rubytalk.org/t/parsing-a-csv-using-ruby-doesnt-split-the-row/43529
**Category:** ruby-talk
**Created:** [5 January 2008 09:56 UTC](https://rubytalk.org/t/parsing-a-csv-using-ruby-doesnt-split-the-row/43529 "2008-01-05T09:56:32Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Raimon\_Fs](https://avatars.discourse-cdn.com/v4/letter/r/b5ac83/32.png) [@Raimon\_Fs](https://rubytalk.org/u/Raimon_Fs)
#### Post date: [5 January 2008 09:56 UTC](https://rubytalk.org/t/parsing-a-csv-using-ruby-doesnt-split-the-row/43529/1 "2008-01-05T09:56:32Z")

</div>

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:

&nbsp;&nbsp;&nbsp;CSV.open(arxiu.file\_path , "r", :col\_sep =\> ?😉 do |row|

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@data\_new=Cat.new  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@data\_new.reporter = row[0]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@data\_new.partner = row[1]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@data\_new.product = row[2]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@data\_new.period = row[3]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@data\_new.imp = row[4]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@data\_new.exp = row[5]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@data\_new.pes\_imp = row[6]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@data\_new.pes\_exp = row[7]

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@data\_new.save

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

### Author: ![Tiziano\_Merzi](https://avatars.discourse-cdn.com/v4/letter/t/ebca7d/32.png) [@Tiziano\_Merzi](https://rubytalk.org/u/Tiziano_Merzi)
#### Post date: [5 January 2008 10:14 UTC](https://rubytalk.org/t/parsing-a-csv-using-ruby-doesnt-split-the-row/43529/2 "2008-01-05T10:14:56Z")

</div>

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:
> 
> &nbsp;&nbsp;&nbsp;CSV.open(arxiu.file\_path , "r", :col\_sep =\> ?😉 do |row|

col\_sep is an optional argument, the call to open is:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CSV.open(arxiu.file\_path , "r", ?😉 do |row|

Tiziano

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

### Author: ![Raimon\_Fs](https://avatars.discourse-cdn.com/v4/letter/r/b5ac83/32.png) [@Raimon\_Fs](https://rubytalk.org/u/Raimon_Fs)
#### Post date: [5 January 2008 10:30 UTC](https://rubytalk.org/t/parsing-a-csv-using-ruby-doesnt-split-the-row/43529/3 "2008-01-05T10:30:16Z")

</div>

> > &nbsp;&nbsp;&nbsp;CSV.open(arxiu.file\_path , "r", :col\_sep =\> ?😉 do |row|
> 
> col\_sep is an optional argument, the call to open is:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CSV.open(arxiu.file\_path , "r", ?😉 do |row|
> 
> Tiziano

thanks, it works now !

regards,

raimon

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

### Author: ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)
#### Post date: [5 January 2008 20:40 UTC](https://rubytalk.org/t/parsing-a-csv-using-ruby-doesnt-split-the-row/43529/4 "2008-01-05T20:40:48Z")

</div>

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:
> 
> > > > &nbsp;&nbsp;CSV.open(arxiu.file\_path , "r", :col\_sep =\> ?😉 do |row|
> > > 
> > > col\_sep is an optional argument, the call to open is:
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;CSV.open(arxiu.file\_path , "r", ?😉 do |row|
> > > 
> > > Tiziano
> > 
> > thanks, it works now !

---

<div class="post-metadata">

### Author: ![Raimon\_Fs](https://avatars.discourse-cdn.com/v4/letter/r/b5ac83/32.png) [@Raimon\_Fs](https://rubytalk.org/u/Raimon_Fs)
#### Post date: [5 January 2008 21:40 UTC](https://rubytalk.org/t/parsing-a-csv-using-ruby-doesnt-split-the-row/43529/5 "2008-01-05T21:40:19Z")

</div>

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/\](http://www.ruby-forum.com/%5C).
