# Faster\_csv column type

**URL:** <https://www.ruby-forum.com/t/faster-csv-column-type/109471>\
**Category:** Ruby\
**Created:** [July 15, 2007, 1:44pm UTC](https://www.ruby-forum.com/t/faster-csv-column-type/109471 "2007-07-15T13:44:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![migrate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bc79bd/32.png) [@migrate](https://www.ruby-forum.com/u/migrate)\
**Post date:** [July 15, 2007, 1:44pm UTC](https://www.ruby-forum.com/t/faster-csv-column-type/109471/1 "2007-07-15T13:44:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![migrate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bc79bd/32.png) [@migrate](https://www.ruby-forum.com/u/migrate)\
**Post date:** [July 15, 2007, 5:24pm UTC](https://www.ruby-forum.com/t/faster-csv-column-type/109471/2 "2007-07-15T17:24:52Z")

</div>

On Jul 15, 2007, at 6:44 AM, Hu Ma wrote:

> Hello all,

Hello.

> Does anyone know how to do this?  
> Does this give you any fresh ideas?

> > require “faster\_csv”  
> > =\> true
> 
> > data = \<\<-END  
> > name,age  
> > James,31  
> > Dana,21  
> > END  
> > =\> “name,age\nJames,31\nDana,21\n”
> 
> > FCSV.parse(data, :headers =\> true, :converters =\> :numeric) do |row|  
> > ?\> p row.inject({}) { |h, (k, v)| h.merge(k =\> v.class) }
> 
> > end  
> > {“name”=\>String, “age”=\>Fixnum}  
> > {“name”=\>String, “age”=\>Fixnum}  
> > =\> nil

James Edward G. II

---

<div class="post-metadata">

**Author:** ![migrate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bc79bd/32.png) [@migrate](https://www.ruby-forum.com/u/migrate)\
**Post date:** [July 22, 2007, 12:11pm UTC](https://www.ruby-forum.com/t/faster-csv-column-type/109471/3 "2007-07-22T12:11:23Z")

</div>

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

James G. wrote:

> On Jul 15, 2007, at 6:44 AM, Hu Ma wrote:
> 
> > Hello all,
> 
> Hello.
> 
> > Does anyone know how to do this?  
> > Does this give you any fresh ideas?
> 
> > > require “faster\_csv”  
> > > =\> true
> > 
> > > data = \<\<-END  
> > > name,age  
> > > James,31  
> > > Dana,21  
> > > END  
> > > =\> “name,age\nJames,31\nDana,21\n”
> > 
> > > FCSV.parse(data, :headers =\> true, :converters =\> :numeric) do |row|  
> > > ?\> p row.inject({}) { |h, (k, v)| h.merge(k =\> v.class) }
> > 
> > > end  
> > > {“name”=\>String, “age”=\>Fixnum}  
> > > {“name”=\>String, “age”=\>Fixnum}  
> > > =\> nil
> 
> James Edward G. II

---

<div class="post-metadata">

**Author:** ![migrate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bc79bd/32.png) [@migrate](https://www.ruby-forum.com/u/migrate)\
**Post date:** [July 26, 2007, 12:46am UTC](https://www.ruby-forum.com/t/faster-csv-column-type/109471/4 "2007-07-26T00:46:31Z")

</div>

On Jul 22, 2007, at 5:11 AM, Hu Ma wrote:

> 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 G. II
