Character encoding issue exporting rails data to CSV

前端 未结 4 1068
后悔当初
后悔当初 2020-12-03 08:12

I\'m exporting data to a CSV file in rails and in some of my fields, I\'m getting character encoding issues like this when I open in Excel:

didn’t
<         


        
4条回答
  •  囚心锁ツ
    2020-12-03 08:38

    This is my approach using I18n.transliterate:

    def self.to_csv(options = {})
      csv = CSV.generate(options) do |csv|
        csv << your_attributes
      end
      I18n.transliterate(csv)
    end
    

    I18n.transliterate just removes strange characters, and tries to make them readable (for example it will replace á with a, ö with o, etc). Just give it a try.

提交回复
热议问题