在我们的应用程序中,我们从不同的来源接收文本文件(.txt, .csv等)。读取时,这些文件有时包含垃圾,因为这些文件是在不同的/未知的代码页中创建的。

是否有一种方法(自动)检测文本文件的代码页?

detectEncodingFromByteOrderMarks,在StreamReader构造函数上,适用于UTF8和其他unicode标记的文件,但我正在寻找一种方法来检测代码页,如ibm850, windows1252。


谢谢你的回答,这就是我所做的。

我们收到的文件来自最终用户,他们没有关于代码页的线索。接收者也是最终用户,到目前为止,这是他们对代码页的了解:代码页存在,并且令人讨厌。

解决方案:

在记事本中打开收到的文件,查看一段乱码的文本。如果有人叫François之类的,凭你的智商你就能猜出来。 我已经创建了一个小应用程序,用户可以使用它打开文件,并输入用户知道它将出现在文件中的文本,当使用正确的代码页时。 遍历所有代码页,并显示包含用户提供的文本的解决方案的代码页。 如果多个代码页同时弹出,请用户指定更多文本。


当前回答

你有没有尝试过c#移植到Mozilla通用字符集检测器

例子来自http://code.google.com/p/ude/

public static void Main(String[] args)
{
    string filename = args[0];
    using (FileStream fs = File.OpenRead(filename)) {
        Ude.CharsetDetector cdet = new Ude.CharsetDetector();
        cdet.Feed(fs);
        cdet.DataEnd();
        if (cdet.Charset != null) {
            Console.WriteLine("Charset: {0}, confidence: {1}", 
                 cdet.Charset, cdet.Confidence);
        } else {
            Console.WriteLine("Detection failed.");
        }
    }
}    

其他回答

你有没有尝试过c#移植到Mozilla通用字符集检测器

例子来自http://code.google.com/p/ude/

public static void Main(String[] args)
{
    string filename = args[0];
    using (FileStream fs = File.OpenRead(filename)) {
        Ude.CharsetDetector cdet = new Ude.CharsetDetector();
        cdet.Feed(fs);
        cdet.DataEnd();
        if (cdet.Charset != null) {
            Console.WriteLine("Charset: {0}, confidence: {1}", 
                 cdet.Charset, cdet.Confidence);
        } else {
            Console.WriteLine("Detection failed.");
        }
    }
}    

StreamReader类的构造函数接受一个“detect encoding”参数。

我使用这段代码来检测Unicode和windows默认的ansi代码页时读取文件。对于其他编码,需要手动或通过编程检查内容。这可以用来保存与打开时相同编码的文本。(我使用VB.NET)

'Works for Default and unicode (auto detect)
Dim mystreamreader As New StreamReader(LocalFileName, Encoding.Default) 
MyEditTextBox.Text = mystreamreader.ReadToEnd()
Debug.Print(mystreamreader.CurrentEncoding.CodePage) 'Autodetected encoding
mystreamreader.Close()

如果您正在寻找检测非utf编码(即没有BOM),那么您基本上需要对文本进行启发式和统计分析。您可能想看一看Mozilla关于通用字符集检测的论文(相同的链接,通过Wayback Machine有更好的格式)。

我实际上是在寻找一种通用的,而不是编程的方法来检测文件编码,但我还没有找到。 通过测试不同的编码,我发现我的文本是UTF-7。

我第一次做的是 StreamReader文件= file . opentext (fullfilename);

我不得不把它改成: StreamReader文件=新的StreamReader(fullfilename, System.Text.Encoding.UTF7);

OpenText假设它是UTF-8。

你也可以像这样创建StreamReader new StreamReader(fullfilename, true),第二个参数意味着它应该尝试从文件的byteordermark中检测编码,但这在我的例子中不起作用。