John Sanchez

Joined

4,620 Experience
23 Lessons Completed
3 Questions Solved

Activity

Posted in SEEKING FREELANCER - Importing CSV file

Sorry, I just realized that if you're using Google Spreadsheets to create and save the CSV file, then you don't need to use the col_sep: '|' option. You can emit those from your the line:

CSV.foreach(filename, headers: true, col_sep: '|') do | row |

So that is reads:

CSV.foreach(filename, headers: true) do | row |

Posted in SEEKING FREELANCER - Importing CSV file

The following line in your stack trace usually means that your CSV file is not encoded to the UTF-8 specifications.

ArgumentError: invalid byte sequence in UTF-8

You would need to specify the encoding you're using in your CSV.foreach line. For example, if you're using MS Excel to save your CSV file, you might try the following:

CSV.foreach(file, "r:windows-1250", headers: true)

Notice the "r:windows-1250" ... That sets the encoding that the CSV library should read.

Or, you could use Google Spreadsheets to create the CSV and then download that file to use. I believe it uses UTF-8 Encoding.

Another thing you should do is explicitly tell Ruby what the column separator is. By default, it's a comma. So, if you use something else, you could throw the CSV library off.

You can do that by adding col_sep: "|" to your foreach line. For example

CSV.foreach('file.csv', headers: false, col_sep: '|')
```

I created my own CSV file with UTF-8 encoding and I tested your code using the following and it works just fine:

```
require 'csv'
namespace :import do

  desc "import research fields from csv"
  task :fields do
    counter = 0

    CSV.foreach('file.csv', headers: true, col_sep: '|') do |row|
      p row
      counter += 1
    end

    puts "Imported #{counter} field codes"
  end
end
```

Try this:

Resave the information in that CSV file using Google Spreadsheets, replace the current one you have, and then try again with the following:

```
require 'csv'
namespace :import do

  desc "import research fields from csv"
  task rannd_fields: :environment do
    filename = File.join Rails.root, 'for_codes.csv'
    counter = 0

    CSV.foreach(filename, headers: true, col_sep: '|') do | row |
      p row["anz_reference"]
      for_code = Randd::Field.create(anz_reference: row["anz_reference"], title: title)
      counter += 1 if randd_field.persisted?
    end
    puts "Imported #{counter} field codes"
  end
end
```

If you receive an error, please post the backtrace here. 

Oh, this is more of a personal preference but you may want to try adding "header_converters: :symbol" to that foreach line as well. It turns your header row into symbols so that you can call them using something like row[:header_name] - For example:

```
CSV.foreach(file, headers: true, header_converters: :symbol)
```

Posted in SEEKING FREELANCER - Importing CSV file

Hey Melanie, Just to be clear, you can't get your code to parse (read) the CSV data or you can't get the CSV file to upload to your server?

What have you tried so far?

Ah!! Very cool. I'll definitely have to give this gem a shot in the next project. =]

Awesome episode! I've used https://rubygems.org/gems/p... but html-pipeline looks like a great alternative. Loving the emojis !!