Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use Csv.open and not File.open? #17

Closed
ldonnet opened this issue Jun 10, 2014 · 1 comment
Closed

use Csv.open and not File.open? #17

ldonnet opened this issue Jun 10, 2014 · 1 comment

Comments

@ldonnet
Copy link

ldonnet commented Jun 10, 2014

Hi,

First, thanks for your greate work! I must deal with a pgsql batch copy but I need to use converters from CSV for hash for examples. I see that you use File.open why not use CSV.open? Is it faster with File.open?
(I use v 0.6 because I'm in rails 3.2)

I need to do this trick for example :

CSV::Converters[:hash] = lambda do |string|
        hash = {}
        begin
          string.gsub!(/[{}]/, '')
          string.split(',').each do |pair|
            key,value = pair.split(/=>/)
            hash[key] = value
          end
        rescue ArgumentError
          string
        end
      end

ActiveRoad::StreetNumber.pg_copy_from "/tmp/street_numbers.csv", :converters => converters
@diogob
Copy link
Owner

diogob commented Jul 30, 2014

Yes, I use the File.open to leave all CSV parsing to the PostgreSQL server. You can transform data passing a block to pg_copy_from and altering the variable that will be yielded with the values.

@diogob diogob closed this as completed Jul 30, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants