faster_csv column type

H

Hu Ma

Hello all,
I'm using faster_csv to import some csv formatted data to a database.
I'm using FasterCSV.foreach method to add all data but I came across
with a problem I don't know how to overcome.

First I convert all csv data using the available converters (and also
adding some others) but before I insert the data to the database I need
to know each column type in order to first create the table and then
insert the data.

Does anyone know how to do this?

Thanks in advance.
Best regards,
Migrate
 
J

James Edward Gray II

Hello all,
Hello.

I'm using faster_csv to import some csv formatted data to a database.
I'm using FasterCSV.foreach method to add all data but I came across
with a problem I don't know how to overcome.

First I convert all csv data using the available converters (and also
adding some others) but before I insert the data to the database I
need
to know each column type in order to first create the table and then
insert the data.

Does anyone know how to do this?

Does this give you any fresh ideas?
name,age
James,31
Dana,21
END
=> "name,age\nJames,31\nDana,21\n"{"name"=>String, "age"=>Fixnum}
{"name"=>String, "age"=>Fixnum}
=> nil

James Edward Gray II
 
H

Hu Ma

Hello James,
Thanks for the answer.

The code that you presented gave me some ideias, however I still don't
know the best way to overcome some problems:
- What is the best way to convert a column to boolean values?
- The csv file can have a column that has both float and integer values
in it. In this case I want to create a float column in the database.
- Is there a way to avoid the duplicating of information? The example
you gave shows the same classes twice.

To better ilustrate the problems here is one sample:
data = <<-END
string,number,boolean
James,32,true
Dana,33.21,false
END

Thanks again.
Best regards,
Migrate
 
J

James Edward Gray II

Hello James,
Hello.

Thanks for the answer.
Sure.

The code that you presented gave me some ideias, however I still don't
know the best way to overcome some problems:
- What is the best way to convert a column to boolean values?

See below.
- The csv file can have a column that has both float and integer
values
in it. In this case I want to create a float column in the database.

You can force the column to a float. See below.
- Is there a way to avoid the duplicating of information? The example
you gave shows the same classes twice.

My example showed the classes for two rows of data. They had the
same classes. Nothing was "duplicated" though.
To better ilustrate the problems here is one sample:
data = <<-END
string,number,boolean
James,32,true
Dana,33.21,false
END

Here's how I would handle that data:

#!/usr/bin/env ruby -wKU

require "rubygems"
require "faster_csv"

data = <<-END
string,number,boolean
James,32,true
Dana,33.21,false
END

BOOLS = {"true" => true, "false" => false}
parsed = FCSV.parse(
data,
:headers => true,
:header_converters => :symbol,
:converters => [
lambda { |f, i| i.header == :number ? Float(f) : f },
lambda { |f, i| i.header == :boolean ? BOOLS[f] : f }
]
)

p parsed.to_a
# >> [[:string, :number, :boolean], ["James", 32.0, true], ["Dana",
33.21, false]]

__END__

Hope that helps.

James Edward Gray II
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,263
Messages
2,571,312
Members
47,987
Latest member
Gaurav

Latest Threads

Top