[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

problem with zipfile.read -> array (line by line

chris

11/8/2007 4:33:00 PM

Hi,

have anybody a hint how i could extract information only for the
first column in a zipped csv.
The hole csv-file is in the array instead line by line as i'm
expected, so i get only the first
character.

#match_f.csv are 3-column and many millions row file.
Zip::ZipFile.open("f:/base_data/match_df.zip",Zip::ZipFile::CREATE)
{ |zipfile|
array=zipfile.read("match_df.csv" ).split("\t")
puts array[0]
}

many thanks for advance
Christian

2 Answers

Peter Szinek

11/8/2007 6:04:00 PM

0

Hi Christian,

> have anybody a hint how i could extract information only for the
> first column in a zipped csv.
> The hole csv-file is in the array instead line by line as i'm
> expected, so i get only the first
> character.
>
> #match_f.csv are 3-column and many millions row file.
> Zip::ZipFile.open("f:/base_data/match_df.zip",Zip::ZipFile::CREATE)
> { |zipfile|
> array=zipfile.read("match_df.csv" ).split("\t")
> puts array[0]
> }

You can also use a csv library (e.g. fasterCSV
http://fastercsv.ruby..., or the standard Ruby one).

If you want to process the file line by line, you should split it on
"\n", then process each line. Code-snippet:

zipfile.read("match_df.csv" ).split.each do |row|
puts row.split(",")[0]
end

In the second split, you should use the delimiter used in the csv file
(\t for instance).


Cheers,
Peter
___
http://www.rubyra...
http://s...





chris

11/8/2007 8:31:00 PM

0

On 8 Nov., 19:04, Peter Szinek <pe...@rubyrailways.com> wrote:
> Hi Christian,
>
> > have anybody a hint how i could extract information only for the
> > first column in a zipped csv.
> > The hole csv-file is in the array instead line by line as i'm
> > expected, so i get only the first
> > character.
>
> > #match_f.csv are 3-column and many millions row file.
> > Zip::ZipFile.open("f:/base_data/match_df.zip",Zip::ZipFile::CREATE)
> > { |zipfile|
> > array=zipfile.read("match_df.csv" ).split("\t")
> > puts array[0]
> > }
>
> You can also use a csv library (e.g. fasterCSVhttp://fastercsv.ruby..., or the standard Ruby one).
>
> If you want to process the file line by line, you should split it on
> "\n", then process each line. Code-snippet:
>
> zipfile.read("match_df.csv" ).split.each do |row|
> puts row.split(",")[0]
> end
>
> In the second split, you should use the delimiter used in the csv file
> (\t for instance).
>
> Cheers,
> Peter
> ___http://www.rubyrailways.comhttp://s...

great! , many thanks
Christian