Notepad ++只称为“ANSI”的编码,是否有人知道如何为Ruby调用它?

我有一堆.txt的Notepad ++说(在它的下拉“编码”菜单中)是“ANSI”。

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

但是当我在File.read 'this is a German text example.txt'时,它们并没有显示在irb中。

那么有谁知道我应该给出什么参数Encoding.default_external=

(我假设这是解决方案,对吗?)

'utf-8''cp850' ,它会将“äöüß”中的“ANSI”文件读作“\ xE4 \ xF6 \ xFC \ xDF”…

(请不要犹豫,在你的答案中提到显而易见的“明显”事物;我几乎和你一样新生,并且仍然知道这个问题就足够了。)

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

我在Notepad ++论坛上找到了这个问题的答案,在2010年由CChris回答,他似乎是权威的。

问题:编码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 

代码页标识符

 Identifier .NET Name Additional information 437 IBM437 OEM United States 

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

在阅读Jörg的答案之后,我回过ruby-doc.org上的编码页面,试图找到他提到的特定编码的引用,那时我发现了Encodings.aliases方法。

所以我在这个答案结束时克服了这个方法。

然后我查看了notepad ++中的输出,将其视为’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" 
Interesting Posts