0

My goal is to upload a file containing rows of firstname and lastname, parse it and create Person model in db for each row.

I do the following and it works fine

file = CSV.parse(the_file_to_parse)
file.each do |row|
  person = Person.new(:firstname => row[0], :lastname => row[1])
  person.save
end

until my file contains accents (french words), I get

Encoding::UndefinedConversionError: "\xC3" from ASCII-8BIT to UTF-8:
INSERT INTO "people" ("created_at", "firstname", "lastname",
"updated_at") VALUES (?, ?, ?, ?)

What is the best way to deal with this encoding issue?

3 Answers 3

2

You need to open the csv file with the right encoding. For example:

require 'csv'
require 'pp'

encoding = "ISO-8859-1"

csv = CSV.open "names.csv", "rb:#{encoding}"
csv.each do |line|
    puts "#{line[0]} #{line[1]}"
end

Here's a hint: it's probably not UTF-8.

The list of encodings that your ruby supports can be viewed with this command in irb:

puts Encoding.list.map(&:to_s).sort
Sign up to request clarification or add additional context in comments.

1 Comment

I've got ArgumentError: 'mode' must be 'r', 'rb', 'w', or 'wb'
0

try to set

# encoding: utf-8  

on the top of ruby file. or

export RUBYOPT=-Ku 

before rails s

Comments

0

If you have no clue about the encoding of the input file you might try ensure_encoding.

It will try to guess the input encoding and try (does not work for all encodings) to convert it to your preferred target encoding.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.