English 中文(简体)
更新与Ruby的档案包裹转换
原标题:Uploaded file char-set conversion with Ruby

我的申请是,我们重新把我们的客户上载到我们的服务器上。 然后,我们处理并把这些星号的数据输入我们的数据库。 我们回头处理一些有焦炭的问题,特别是当我们重新处理森特时,特别是一些没有改变的UTF-8型特征正在打破爱琴公司的反应。

是否有办法在我们开始处理之前将上载的茨夫档案转换到UTF-8? 是否有办法确定上载文件的性质? 我在一旁玩笑了,但我们并不总是能够肯定的是,上载档案会有哪些内容。 感谢。

最佳回答

这种解决办法可能并不理想,但应当做这项工作。

第一,成分:

  • chardet (sudo gem install chardet)
  • fastercsv (sudo gem install fastercsv)

现行法典(未测试):

require  rubygems 
require  UniversalDetector 
require  fastercsv 
require  iconv 

file_to_import = File.open("path/to/your.csv")
# determine the encoding based on the first 100 characters
chardet = UniversalDetector::chardet(file_to_import.read[0..100])
if chardet[ confidence ] > 0.7
  charset = chardet[ encoding ]
else 
  raise  You better check this file manually. 
end
file_to_import.each_line do |l| 
  converted_line = Iconv.conv( utf-8 , charset, l)
  row = FasterCSV.parse(converted_line)[0]
  # do the business here
end
问题回答

暂无回答




相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

热门标签