2013-04-18 23 views
15

我有一堆记事本++说的(在它的下拉“编码”菜单中)是“ANSI”.txt。记事本++只是调用“ANSI”的编码,有人知道该为Ruby调用它吗?

他们有德语字符,[äöüß],它在Notepad ++中显示得很好。

但他们没有出现在irb当我File.read 'this is a German text example.txt'他们。

因此,有谁知道我应该给什么论据Encoding.default_external=

(我假设会成为解决方案,对吧?)

'utf-8''cp850',它读取它的 “ANSI” 与 “äöüß” 文件“\ XE4 \ XF6 \ XFC \ XDF” ......

(请不要犹豫,何况显然是‘显而易见’的东西在你的答案。我非常多的newbish,尽你所能,仍然知道刚够问这个问题)

+0

这取决于你的操作系统语言环境。对于德语或英语,它是Windows-1252。虽然Notepad ++可能不会遵循这一点,只是将它用作Windows-1252的别名。这当然不是任何ISO编码。见http://en.wikipedia.org/wiki/Windows_ANSI_code_page#ANSI_code_page – Esailija

+0

谢谢,我认为这是cp1252,是的。 –

回答

10

它们的含义可能是ISO/IEC 8859-1(又名Latin-1),ISO-8859-1,ISO/IEC 8859-15(又名Latin-9)或Windows-1252(又名CP 1252)。他们中的所有4人都在位置0xE4处有ä

+1

谢谢,我认为这是cp1252,是的。 –

3

我认为这是'cp1252',别名'windows-1252'。

阅读Jörg的回答后,我回到ruby-doc.org上的Encoding页面,试图找到他提到的具体编码的参考,这就是当我发现Encodings.aliases方法。

所以我在这个答案的最后对这个方法进行了分析。

然后我看着在记事本+ +的输出,将其视为两个“ANSI”和UTF-8,和比较,要在IRB输出...

我只能找到在IRB输出两个地方其中utf-8文件与查看“ANSI”时出现在记事本++中的方式完全相同,并且这些地方用于cp1252和cp1254。

cp1252显然是我的'文件系统'编码,所以我就这样做了。

我写了一个脚本,以使所有的文件的副本转换为UTF-8的,试图从两者1252和1254

UTF-8正则表达式似乎有两套文件至今工作。

现在我必须尝试记住我在尝试完成之前完成之前遇到过所有这些编码问题。的xD

def compare_encodings file1, file2 
    file1_probs = [] 
    file2_probs = [] 

    txt = File.open('encoding_test_output.txt','w') 

    Encoding.aliases.sort.each do |k,v| 
     Encoding.default_external=k 
     ename = [k.downcase, v.downcase].join " --- " 
     s = "" 
     begin 
      s << "#{File.read(file1)}" 
     rescue 
      s << "nope nope nope" 
      file1_probs << ename 
     end 
     s << "\t| #{ename} |\t" 
     begin 
      s << "#{File.read(file2)}" 
     rescue 
      s << "nope nope nope" 
      file2_probs << ename 
     end 
     Encoding.default_external= 'utf-8' 
     txt.puts s.center(58) 
     puts s.center(58) 
    end 
    puts 
    puts "file1, \"#{file1}\" exceptions from trying to convert to:\n\n" 
    puts file1_probs 
    puts 
    puts "file2, \"#{file2}\" exceptions from trying to convert to:\n\n" 
    puts file2_probs 
    txt.close 
end 

compare_encodings "utf-8.txt", "np++'ANSI'.txt" 
5

我找到了答案,在记事本++论坛这个问题,在2010年回答CChris谁似乎是权威的。

Question: Encoding ANSI?

答:

这将是为您的计算机(代码页0)系统代码页。

更多信息:

显示当前代码页。

>help chcp 
Displays or sets the active code page number. 

CHCP [nnn] 

    nnn Specifies a code page number. 

Type CHCP without a parameter to display the active code page number. 

>chcp 
Active code page: 437 

Code Page Identifiers

Identifier .NET Name Additional information 
437   IBM437  OEM United States 
相关问题