2017-10-06 136 views
-2

我看到很多關於排序製表符分隔的主題,但一直無法掌握或理解完成這段代碼的任務以及我剛剛在C#中開始的任務。我希望有人能回答這個問題。c#對一個製表符分隔的文件進行排序

我想打開一個文本文件,它是用特定數量的字段製表符分隔的。問題是如何將它與第一列字段進行排序,然後使用第二列進行排序。我希望能夠查看列表數組中的字段以進行調試,如果可能的話。我希望這個示例以tab分隔的形式出現。 那當然我想能寫回來。

Category Name Category Sub Name Family Sales Description Equipment Tag List Price Price ID 
Fixture Type 2 Basket Sales B2 65 64 366589 
Fixture Type 2 Basket Sales B2 65 64 366595 
Fixture Type 2 Basket Sales B2 65 64 366601 
Fixture Type 2 Basket Sales B2 65 64 366607 
Fixture Type 2 Basket Sales B2 65 64 366613 
Fixture Type 22 Rail Sales X1 10 10 382822 
Device Type 1 Wall Outside Null 360 342 400604 
Device Type 3 Standard Outside Null 180 171 400885 
Device Type 1 Wall Outside Null 360 342 400965 
Device Type 1 Wall Outside Null 360 342 401034 
Device Type 1 Wall Outside Null 360 342 401303 
Device Type 3 Standard Standard Null 180 171 401471 
Device Type 1 Wall Outside Null 360 342 401596 
Device Type 3 Standard Standard Null 180 171 401753 
Device Type 3 Standard Standard Null 180 171 401866 
Device Type 1 Wall Outside Null 360 342 402189 
Device Type 3 Standard Standard Null 180 171 402537 
Device Type 1 Wall Outside Null 360 342 402685 
Device Type 1 Wall Outside Null 360 342 402930 
Device Type 1 Wall Outside Null 360 342 402952 
Device Type 3 Standard Standard Null 180 171 403164 
Device Type 1 Wall Outside Null 360 342 403234 
Device Type 3 Standard Standard Null 180 171 403303 
Device Type 1 Wall Outside Null 360 342 403473 
Fixture Type 4 Standard Null F1 140 137 406101 
Fixture Type 4 Step Null F1 140 137 406102 
Fixture Type 4 Step Null F1 140 137 406103 
Fixture Type 4 Step Null F1 140 137 406104 
Fixture Type 4 Step Null F1 140 137 406105 
Fixture Type 4 Step Null F1 140 137 406106 
Fixture Type 4 Step Null F1 140 137 406124 
Fixture Type 4 Step Null F1 140 137 406125 
Fixture Type 4 Step Null F1 140 137 406126 
Fixture Type 4 Step Null F1 140 137 406127 
Fixture Type 4 Step Null F1 140 137 406128 
Fixture Type 4 Step Null F1 140 137 406129 
+0

你不能只是要求人們爲你做這項工作。你到目前爲止嘗試過哪些方法不適合你?你有沒有在你嘗試過的地方尋找解決方案? –

+0

我明白你的意思了。好點子。我似乎無法發佈任何編碼的樣本,我一直在這裏搜索幾天。 –

+0

這是一個常見的谷歌搜索,我回來的所有結果都沒有提供任何解決方案,似乎適合我的情況。 「閱讀和排序一個csv c#」 –

回答

1

如上所述 - 你不能指望人們爲你做這件事...但我很無聊。

下面是一個完整的控制檯應用程序形式的簡單解決方案,可能會分崩離析第二次給它真實世界的數據,但希望能讓你開始。

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace ConsoleApp1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
     //Read file 
     var fileContents = File.ReadAllText("file.txt"); 

     //split on carriage returns and line feeds, remove empty entries. 
     var lines = fileContents.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); 

     //Split each line on Tab 
     var splitLines = lines.Select(l => l.Split(new[] { '\t' }, StringSplitOptions.RemoveEmptyEntries)); 

     //splitLines is now an array of arrays. Each splitLine entry is a line, and each entry of each splitline element is 
     //a single field... so we should be able to sort how we want, e.g. by first field then by second field: 
     var sortedLines = splitLines.OrderBy(sl => sl[0]).ThenBy(sl => sl[1]); 

     //put back together as TSV - put tabs back. 
     var linesWithTabsAgain = sortedLines.Select(sl => string.Join("\t", sl)); 

     //put carriage returns/linefeeds back 
     var linesWithCRLF = string.Join("\r\n", linesWithTabsAgain); 

     File.WriteAllText("newFile.txt",linesWithCRLF); 


    } 
} 
} 
+0

:)你讓我的日子。非常感謝你的提交。很棒。我已經研究了近一個星期,但從未發現任何東西。 –

+0

我希望有人發現它和我一樣有用。 –

+0

不客氣。而不是對數據進行排序,而是將其轉換爲可排序的數據,對其進行排序然後再將其轉換回來。很明顯,如果源數據是巨大的,因爲它將整個內容加載到內存中,這種方法並不會很好......如果您有內存問題,最好將它加載到數據庫中進行排序......但它是一個週五下午有趣的小鍛鍊:) – GPW

相關問題