Rails csv 导入编码的问题.

Thomastar · July 09, 2018 · Last by luikore replied at July 14, 2018 · 1481 hits
csv_text = params[:csv].tempfile
# encoding
# csv = File.read(csv_text.path, encoding: 'utf-16')
csv = File.read(csv_text.path, encoding: "UTF-16LE:UTF-8")
puts '==encoding==='
p csv.encoding
p csv

csv = CSV.parse(csv, encoding: "UTF-16LE:UTF-8")
csv.each_with_index do |item, index|
 puts '==='
 p item
end

估计是编码的问题,文件的 encoding : UTF -8

数据是阿里巴巴导出的 csv 数据

CSV.parse 就不用再加 encoding 参数了

You need to Sign in before reply, if you don't have an account, please Sign up first.