c# 对制表符分隔的文件进行排序
c# sort a tab delimited file
我看过很多关于排序制表符分隔的主题,但一直无法掌握或理解完成这段代码的任务,而且我刚刚开始使用 C#。希望有人能回答这个问题。
我想打开一个文本文件,该文件以制表符分隔并带有特定数量的字段。问题是如何使用第一列字段然后使用第二列对其进行排序。如果可能的话,我希望能够查看列表数组中的字段以进行调试。我希望这个示例以制表符分隔的形式出现。
那我当然希望能写回来
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
如上所述 - 你真的不能指望别人为你做这些工作......但我很无聊。
这是一个完整的控制台应用程序形式的简单解决方案,它可能会在您向其提供真实世界数据的那一刻崩溃,但希望能帮助您入门。
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);
}
}
}
我看过很多关于排序制表符分隔的主题,但一直无法掌握或理解完成这段代码的任务,而且我刚刚开始使用 C#。希望有人能回答这个问题。
我想打开一个文本文件,该文件以制表符分隔并带有特定数量的字段。问题是如何使用第一列字段然后使用第二列对其进行排序。如果可能的话,我希望能够查看列表数组中的字段以进行调试。我希望这个示例以制表符分隔的形式出现。 那我当然希望能写回来
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
如上所述 - 你真的不能指望别人为你做这些工作......但我很无聊。
这是一个完整的控制台应用程序形式的简单解决方案,它可能会在您向其提供真实世界数据的那一刻崩溃,但希望能帮助您入门。
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);
}
}
}