PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > parsing a csv using ; => ruby doesn't split the row
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
parsing a csv using ; => ruby doesn't split the row

Réponse
 
LinkBack Outils de la discussion
Vieux 05/01/2008, 09h56   #1
Raimon Fs
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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 => ? 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/.

  Réponse avec citation
Vieux 05/01/2008, 10h14   #2
Tiziano Merzi
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: parsing a csv using ; => ruby doesn't split the row

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 => ? do |row|


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

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

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

  Réponse avec citation
Vieux 05/01/2008, 10h30   #3
Raimon Fs
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: parsing a csv using ; => ruby doesn't split the row


>> 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



thanks, it works now !

regards,

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

  Réponse avec citation
Vieux 05/01/2008, 20h40   #4
James Gray
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: parsing a csv using ; => ruby doesn't split the row

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

>
>>> 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

>
>
> 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

  Réponse avec citation
Vieux 05/01/2008, 21h40   #5
Raimon Fs
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: parsing a csv using ; => ruby doesn't split the row

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/.

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 19h16.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11160 seconds with 13 queries