Ruby read CSV file as UTF-8 and/or convert ASCII-8Bit encoding to UTF-8

后端 未结 3 2181
醉酒成梦
醉酒成梦 2020-12-04 15:39

I\'m using ruby 1.9.2

I\'m trying to parse a CSV file that contains some French words (e.g. spécifié) and place the contents in a MySQL database.

3条回答
  •  无人及你
    2020-12-04 15:55

    I have been dealing with this issue for a while and not any of the other solutions worked for me.

    The thing that made the trick was to store the conflictive string in a binary File, then read the File normally and using this string to feed the CSV module:

    tempfile = Tempfile.new("conflictive_string")
    tempfile.binmode
    tempfile.write(conflictive_string)
    tempfile.close
    cleaned_string = File.read(tempfile.path)
    File.delete(tempfile.path)
    csv = CSV.new(cleaned_string)
    

提交回复
热议问题