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 comma-separated file
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Parsing a comma-separated file

Réponse
 
LinkBack Outils de la discussion
Vieux 09/06/2008, 17h11   #1
Justin To
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Parsing a comma-separated file

Hi, I had a question about parsing just one line at a time beforehand
and now I'm working on a program to parse multiple items on each
line-something like the following:

name, age, gender
Bob, 32, M
Stacy, 14, F
...
...

How do I parse 'Bob', knowing it's the first element on the line, '32'
is the second, 'M' is the last...I've been reading about regular
expressions. Is this the best way to solve this problem? And how exactly
do you use them?

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

  Réponse avec citation
Vieux 09/06/2008, 17h24   #2
ThoML
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Parsing a comma-separated file

Are you looking for this?
http://fastercsv.rubyforge.org/

Ruby also has the csv standard library.

Regards,
Thomas.
  Réponse avec citation
Vieux 09/06/2008, 17h46   #3
Justin To
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Parsing a comma-separated file

ThoML wrote:
> Are you looking for this?
> http://fastercsv.rubyforge.org/
>
> Ruby also has the csv standard library.
>
> Regards,
> Thomas.


That is great Thomas! Although, I'd like to know how to do it with the
regular expressions as well.

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

  Réponse avec citation
Vieux 09/06/2008, 17h56   #4
Avdi Grimm
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Parsing a comma-separated file

On Mon, Jun 9, 2008 at 12:46 PM, Justin To <tekmc@hotmail.com> wrote:
> That is great Thomas! Although, I'd like to know how to do it with the
> regular expressions as well.


I'd recommend using Sring#split. In the simplest case you could just
specify line.split(','); no regular expressions needed. If you wanted
you could use a regular expression argument to #split in order to skip
whitespace:

line.split(/\s*,\s*/)

but you could just as easily trim the values after the fact too:

line.split(',').map{|v| v.strip}


Regular expressions are not the best solution for parsing CSV,
especially once you start dealing with quoted values.

--
Avdi

Home: http://avdi.org
Developer Blog: http://avdi.org/devblog/
Twitter: http://twitter.com/avdi
Journal: http://avdi.livejournal.com

  Réponse avec citation
Vieux 09/06/2008, 19h08   #5
Justin To
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Parsing a comma-separated file

So is the fasterCSV the most effective way of parsing a comma-separated
file?

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

  Réponse avec citation
Vieux 09/06/2008, 20h09   #6
Avdi Grimm
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Parsing a comma-separated file

On Mon, Jun 9, 2008 at 2:08 PM, Justin To <tekmc@hotmail.com> wrote:
> So is the fasterCSV the most effective way of parsing a comma-separated
> file?


It is the fastest and most robust way.

--
Avdi

Home: http://avdi.org
Developer Blog: http://avdi.org/devblog/
Twitter: http://twitter.com/avdi
Journal: http://avdi.livejournal.com

  Réponse avec citation
Vieux 09/06/2008, 22h52   #7
Charles Walden
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Parsing a comma-separated file

My experience (at least a year ago) was that fastercsv was a great way
to go if you had very clean files without errors, odd characters,
etc. Unfortunately, I had files that were a bit more problematic and
so I ended up using a combination of either parsing it myself (split,
regexs. etc) and catching all the errors and handling them or using
the parse_line method in the standard csv library.
On Jun 9, 2008, at 2:09 PM, Avdi Grimm wrote:

> On Mon, Jun 9, 2008 at 2:08 PM, Justin To <tekmc@hotmail.com> wrote:
>> So is the fasterCSV the most effective way of parsing a comma-
>> separated
>> file?

>
> It is the fastest and most robust way.
>
> --
> Avdi
>
> Home: http://avdi.org
> Developer Blog: http://avdi.org/devblog/
> Twitter: http://twitter.com/avdi
> Journal: http://avdi.livejournal.com
>



  Réponse avec citation
Vieux 09/06/2008, 23h53   #8
Justin To
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Parsing a comma-separated file

Great guys, thanks for the !
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 09/06/2008, 23h55   #9
Greg Willits
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Parsing a comma-separated file

> name, age, gender
> Bob, 32, M
> Stacy, 14, F
> ...
> How do I parse 'Bob', knowing it's the first element on the line, '32'
> is the second, 'M' is the last...I've been reading about regular
> expressions. Is this the best way to solve this problem? And how exactly
> do you use them?


This doesn't handle all CSV specs, but if you know you have pure data
like you show above, these are the rudimentary steps without the
one-liner tricks, so it should be pretty straight forward to understand
each step. Arranging them as methods to a class would be good.


# read the file into a var

if FileTest::exist?(file_name)
file_lines = IO.readlines(file_name)
end

# normalize line endings so it doesn't matter what they are

file_lines.strip!
file_lines.gsub!(/\r\n/,'\n')
file_lines.gsub!(/\r/,'\n')

# normalize comma delimiters so it doesn't matter
# if you have one, two or one,two or one , two etc...

file_lines.gsub!(/\s*,\s*/, ',')

# split lines into a single array of lines

lines_array = file_lines.split('\n')

# split each line into an array

final_data = []

lines_array.each do |this_line|
final_data << this_line.split(',')
end

# final_data is now an array of arrays that looks like this:

[
['name', 'age', 'gender'],
['Bob', '32', 'M'],
['Stacy', '14', 'F']
]

So, to get Bob, you'd have to know his line number, and index into the
record array:

final_data[1][0] # Bob
final_data[2][3] # F


-- greg willits

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

  Réponse avec citation
Vieux 10/06/2008, 00h26   #10
James Gray
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Parsing a comma-separated file

On Jun 9, 2008, at 4:52 PM, Charles Walden wrote:

> My experience (at least a year ago) was that fastercsv was a great
> way to go if you had very clean files without errors, odd
> characters, etc. Unfortunately, I had files that were a bit more
> problematic and so I ended up using a combination of either parsing
> it myself (split, regexs. etc) and catching all the errors and
> handling them or using the parse_line method in the standard csv
> library.


FasterCSV has a parse_line() method as well, just FYI.

James Edward Gray II

  Réponse avec citation
Vieux 10/06/2008, 00h54   #11
Justin To
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Parsing a comma-separated file


>
> final_data[1][0] # Bob
> final_data[2][3] # F
>


should the last one be:
final_data[2][2] # F
??

Thanks! Also, is this an effective way to parse a large file. What if I
had to read a million lines with multiple columns? Would this solution
still be practical?

Thanks again!

--
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 06h32.


É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,20691 seconds with 19 queries