Forums | Admin

Discussion Forums: help

Start New Thread Start New Thread

 

By: Thibaut Barrère
RE: Row preprocessing ? [ reply ]  
2009-09-04 19:28
well sure, it makes sense - I just tend to avoid writing code if a single library call makes the job.

thanks for the code, I will definitely use this - and thanks for replying so quickly :)

-- Thibaut

By: James Gray
RE: Row preprocessing ? [ reply ]  
2009-09-04 19:22
Luckily, handling the headers is very easy in your case. For example:

data = <<END_DATA
a:b:c
1:2:3
4:5:6
END_DATA

headers = nil
data.each do |row|
row.strip!
if headers.nil?
headers = row.split(":")
else
fields = Hash[*headers.zip(row.split(":")).flatten]
p fields
end
end

I'm not trying to run you off, of course. It's just that you are feeding non-CSV data to a CSV parser. It's not too surprising this is problematic. Your data is very easy to read without help though.

I hope that makes sense.

By: Thibaut Barrère
RE: Row preprocessing ? [ reply ]  
2009-09-04 19:09
Well the fact that FasterCSV handles the headers nicely and returns a hash for each record made me use it in this case, even if I don't really need a parser, you're right :)

I could rewrite the code to achieve the same thing - but I wanted to ensure there wasn't any quick way to do that still with FasterCSV.

thoughts ?


By: James Gray
RE: Row preprocessing ? [ reply ]  
2009-09-04 19:01
Is there any reason you even need a CSV parser? It looks like this code would work for you:

fields = line.split(":")

James Edward Gray II

By: Thibaut Barrère
Row preprocessing ? [ reply ]  
2009-09-04 18:52
Hi,

I was wondering: is there some built-in way (converters maybe ?) to preprocess the content of each row before it gets parsed by FasterCSV ?

My content hasn't got any quotes in it:

id;name
7;Bob David
8;John "Amazing" Bowie

Until today I had no lines with double quotes in there, it's a new use case. The line with id 8 causes an Illegal quoting.

I tried setting :quote_char to nil but it needs a one-char string.

Is there some way to achieve this internally, or should I just preprocess the quote before myself (I don't need it at all) ?

thanks for your support (and work on FasterCSV),

Thibaut
--
http://www.learnivore.com