Rails Import CSV Error: Invalid Byte Sequence in UTF-8

I get an error invalid byte sequence in UTF-8when trying to import a CSV file into my Rails application. Everything worked fine until I added a method gsubto compare one of the CSV columns with a field in my database.

When I import a CSV file, I want to check if the address for each line is included in an array of different addresses for a particular client. I have a client model with a property alt_addressesthat contains several different possible formats for the client address.

Then I have a citation model (if you are familiar with local SEO, you will know this term). The citation model does not have an address field, but has a field nap_correct?(NAP means "name", "address", "phone number"). If the name, address and phone number for the CSV string are equivalent to what I have in the database for this client, the field nap_correct?for this quote will be set to "correct".

Here is what the method looks like importin my citation model:

def self.import(file, client_id)
  @client = Client.find(client_id)
  CSV.foreach(file.path, headers: true) do |row|
    @row = row.to_hash
    @citation = Citation.new
    if @row["Address"]
      if @client.alt_addresses.include?(@row["Address"].to_s.downcase.gsub(/\W+/, '')) && self.phone == @row["Phone Number"].gsub(/[^0-9]/, '')
        @citation.nap_correct = true
      end
    end
    @citation.name = @row["Domain"]
    @citation.listing_url = @row["Citation Link"]
    @citation.save
  end
end

And here the property looks alt_addressesin my client model:

def alt_addresses
  address = self.address.downcase.gsub(/\W+/, '')
  address_with_zip = (self.address + self.zip_code).downcase.gsub(/\W+/, '')
  return [address, address_with_zip]
end

gsub, CSV, , . . gsub, .

Ruby 2.1.3. , , , , Ruby.

+4
2

encoding:

CSV.foreach(file.path, headers: true, encoding: 'iso-8859-1:utf-8') do |row|
 # your code here
end
+13

, , - " " Open office libre, " ", , UTF-8 . , utf-8, ​​. f-ing

unix, iconv, , , . https://superuser.com/questions/588048/is-there-any-tools-which-can-convert-any-strings-to-utf-8-encoded-values-in-linu

0

Source: https://habr.com/ru/post/1611131/


All Articles