Rails 3,导入前检查CSV文件编码 [英] Rails 3, check CSV file encoding before import

查看:230
本文介绍了Rails 3,导入前检查CSV文件编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序(Rails 3.0.5,Ruby 1.8.7)中,我创建了一个导入工具,用于从文件导入CSV数据。

In my app (Rails 3.0.5, Ruby 1.8.7), I created an import tool to import CSV data from file.

问题:用户可以使用UTF-8编码从Excel导出CSV文件,但是它们大多数时候不会这样做。

Problem: I asked my users to export the CSV file from Excel in UTF-8 encoding but they don't do it most of time.

如何验证文件是否为UTF- 8进口前?否则导入将运行,但给出奇怪的结果。我使用FasterCSV导入。

How can I just verify if the file is UTF-8 before importing ? Else the import will run but give strange results. I use FasterCSV to import.

错误的CSV文件示例:

Exemple of bad CSV file:

;VallÈe du RhÙne;CÙte Rotie;

谢谢。

推荐答案

您可以使用Charlock Holmes,Ruby的字符编码检测库。

You can use Charlock Holmes, a character encoding detecting library for Ruby.

https://github.com/brianmario/charlock_holmes

要使用它,您只需读取该文件,并使用检测方法。

To use it, you just read the file, and use the detect method.

contents = File.read('test.xml')
detection = CharlockHolmes::EncodingDetector.detect(contents)
# => {:encoding => 'UTF-8', :confidence => 100, :type => :text}

如果格式不正确,您还可以将编码转换为UTF- :

You can also convert the encoding to UTF-8 if it is not in the correct format:

utf8_encoded_content = CharlockHolmes::Converter.convert contents, detection[:encoding], 'UTF-8'

这样可以节省用户在重新上传之前自己进行操作。

This saves users from having to do it themselves before uploading it again.

这篇关于Rails 3,导入前检查CSV文件编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆