2016-08-05 29 views
-1

B「H製表符分隔的文件解析什麼是應該在C#

我應該是製表符分隔的文件。Excel打開它精細沒有問題。但是當我嘗試File.ReadAllText( )我無法得到一個體面的代表 我可以做的最好的是UTF8返回大部分數據,但是第一行全部搞砸了,文檔其餘部分的一些標籤丟失了。這是使用UTF8讀取的第一行: ? 0 \ 0 \ 0 \ 0 \ 0 \ U0001 \ 0 \ 0 \ 0ID \ 0 \ 0 \ 0 \ 0 \ 0 \ 0℃\ 0 \ 0 \ 0 \ 0 \ u0006 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0NAME \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0℃\ 0 \ 0 \ 0 \ 0 \ u001e \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0ADDR \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0℃\ 0 \ 0 \ 0 \ 0(\ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0ADDRC \ 0 \ 0 \ 0 \ 0 \ 0 \ 0℃\ 0 \ 0 \ 0 \ 0(\ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0CITY \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0℃\ 0 \ 0 \ 0 \ 0 \ u001e \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0STATE \ 0 \ 0 \ 0 \ 0 \ 0 \ 0℃\ 0 \ 0 \ 0 \ 0 \ u0014 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0ZIP \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0L \ 0 \ 0 \ 0 \ 0 \ 00001 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \在記事本中打開時顯示的幾個字節: !!A A

有沒有人認識到編碼?

+1

你試過'StreamReader.CurrentEncoding'? – Rahul

+0

StreamReader.CurrentEncoding僅適用於標準編碼。這些文件顯然不是標準的。 – Rabbi

+0

@ peter-duniho此問題不重複。它甚至與你發佈的問題沒有關係。該問題詢問您將如何通過編程從標準編碼的小列表中找到編碼。我不需要程序檢測。我需要幫助確定這種特定的編碼。 – Rabbi

回答

1

首先,我們來檢查一下編碼相關問題的可能性,它是純文本文件的禍害。使用Microsoft Word或Notepad ++通過預覽每一個來發現編碼。

在Microsoft Word中,進入菜單「選項」,「高級」,「常規」小節,並在「打開時確認文件格式轉換」之外進行檢查。完成後,點擊確定按鈕。然後,在Microsoft Word中打開該文件。預覽每個編碼,直到找到能正確顯示所有內容的編碼。

一旦找到編碼,請使用.NET Framework to open the file with that encodingStreamReader類。

+0

謝謝。 Word和NotePad ++是偉大的想法。他們都不能正確打開文件。每個人都提供了大量的編碼選項,但沒有一個能夠正確顯示文件。現在,Excel不會顯示該文件。問題是我有一堆這樣的文件,所以我需要弄清楚它是什麼編碼,以便我可以在語法上讀取這些文件。我無法在Excel中找到一個能夠告訴我使用什麼編碼打開文件的地方。 – Rabbi

+0

@Rabbi:這當然很奇怪。有可能你擁有的實際上是一個excel可識別的二進制文件,根本不是純文本文件。您可以嘗試將它們從Excel導出到實際的製表符分隔文件中。另外,我可以爲你分析其中的一個文件,但是,出於隱私原因你可能不想這樣做。 –

-1

到目前爲止,這種獲取文件編碼的方式對我來說很好。

http://weblog.west-wind.com/posts/2007/Nov/28/Detecting-Text-Encoding-for-StreamReader

/// <summary> 
    /// Detects the byte order mark of a file and returns 
    /// http://weblog.west-wind.com/posts/2007/Nov/28/Detecting-Text-Encoding-for-StreamReader 
    /// an appropriate encoding for the file. 
    /// </summary> 
    /// <param name="srcFile"></param> 
    /// <returns></returns> 
    public static Encoding GetFileEncoding(string srcFile) 
    { 
     // *** Use Default of Encoding.Default (Ansi CodePage) 
     Encoding enc = Encoding.Default; 
     // *** Detect byte order mark if any - otherwise assume default 
     byte[] buffer = new byte[5]; 
     FileStream file = new FileStream(srcFile, FileMode.Open); 
     file.Read(buffer, 0, 5); 
     file.Close(); 

     if (buffer[0] == 0xef && buffer[1] == 0xbb && buffer[2] == 0xbf) 
      enc = Encoding.UTF8; 
     else if (buffer[0] == 0xfe && buffer[1] == 0xff) 
      enc = Encoding.Unicode; 
     else if (buffer[0] == 0 && buffer[1] == 0 && buffer[2] == 0xfe && buffer[3] == 0xff) 
      enc = Encoding.UTF32; 
     else if (buffer[0] == 0x2b && buffer[1] == 0x2f && buffer[2] == 0x76) 
      enc = Encoding.UTF7; 
     return enc; 
    } 

我使用這樣的

//To read 
Encoding currentFileEnc = GetFileEncoding(TheFile); 
using (StreamReader sr = new StreamReader(TheFile, currentFileEnc)) 
{ 
    //Blah blah blah 
} 

//To write back 
using (StreamWriter sw = new StreamWriter(TempFilePath, false, currentFileEnc)) 
{ 
    //blah blah blah 
} 
+0

謝謝。正如我在問題中所說的那樣。這些文件不是任何標準的編碼。我已經嘗試了所有的常客,並且我沒有收到可用的文件。另一方面,Excel打開它們很好。我只需要知道如何在語法上做到這一點。一旦我確定了這種編碼,我就不需要用語法來檢查它 - 我只需要編寫(或查找)一個轉換函數。 – Rabbi

+0

你的問題的標題是「你怎麼能找到一個文件的編碼c#」 –

+0

是的我需要一種方法來找到這個特定的文件的編碼。這不是一個標準的編碼。請閱讀問題的正文。 – Rabbi