如何在 .net 中顺序处理文本文件时检查另一列值
How to check another column value while processing textfile sequentially in .net
我正在阅读包含 10 万个条目的文本文件,其中很少有问题张贴。我必须只读取那些活跃的记录 'Y' 其余的我必须跳过,如果 ASC
的列值是 ASC10
或 ASC20
那么我必须设置一个字符串名为 'sss' 的变量值为 "Flori",如果 ASC30
那么我必须检查其他名为 'SAC' 的列,如果它的值为 3 那么 sss="Texi" 否则如果它是 4 那么 sss="Flori".
以下代码将读取文本文件并进行处理,但我无法实现这两个要求,因为值在同一行中并按顺序检索值
我写的代码是:
private static void Readfiles()
{
string path = @"D:\study\Students.txt";
string sss = string.Empty;
System.Collections.ArrayList ar = new System.Collections.ArrayList();
string[] lines = File.ReadAllLines(path).Where(arg => !string.IsNullOrWhiteSpace(arg)).ToArray();
string[] cols = lines[0]
.Trim()
.Split(new[] { '\t', ' ' }, StringSplitOptions.RemoveEmptyEntries);
int liness = 1;
foreach (string line in lines.Skip(1))
{
string[] cells = line
.Trim()
.Split(new[] { '\t' }, StringSplitOptions.RemoveEmptyEntries);
for (int counter = 0; counter < cols.Length; counter++)
{
string cellValue = "N/A";
if (counter < cells.Length)
cellValue = cells[counter];
Console.WriteLine("{0} {1}", cols[counter],cellValue);
if (cols[counter] == "ASC")
{
if (cellValue == "ASC10" || cellValue == "ASC20")
{
sss = "Flori";
}
//Here i have to check other column named "SAC" but HOWWWWWWWWWWWWWW??????????????????????????????????/ because processing is sequential
if (cellValue == "ASC30")
{
sss = "Texi";
}
}
}
liness++;
}
}
文本文件格式为
Firstname lastname ASC age salary location active SAC
Tom jerry ASC10 32 20000 NY Y 3
Sam peter ASC20 31 30000 KY N 4
jason sam ASC30 21 40000 JU Y 3
jerry Forman ASC20 34 23456 KK Y 4
要跳过非活动行,请获取 "active" 列的索引并在 foreach 循环开始时检查该值:
int indexActive = Array.indexOf(cols, "active");
if (indexActive >= 0 && indexActive < cells.Count() && cells[indexActive] == "N")
{
continue; // this skips this round of the foreach loop and continues with the next one
}
获取 SAC 列的索引并检查当前行中具有该索引的单元格,如下所示(在您的评论处插入):
int indexSAC = Array.indexOf(cols, "SAC");
if (indexSAC >= 0 && indexSAC < cells.Count())
{
if (cells[indexSAC] == "3")
{
sss = "Texi";
}
else if (cells[indexSAC] == "4")
{
sss = "Flori";
}
}
第一行也可以在您定义 cols
数组之后插入,这样您只需要执行一次。
我正在阅读包含 10 万个条目的文本文件,其中很少有问题张贴。我必须只读取那些活跃的记录 'Y' 其余的我必须跳过,如果 ASC
的列值是 ASC10
或 ASC20
那么我必须设置一个字符串名为 'sss' 的变量值为 "Flori",如果 ASC30
那么我必须检查其他名为 'SAC' 的列,如果它的值为 3 那么 sss="Texi" 否则如果它是 4 那么 sss="Flori".
以下代码将读取文本文件并进行处理,但我无法实现这两个要求,因为值在同一行中并按顺序检索值
我写的代码是:
private static void Readfiles()
{
string path = @"D:\study\Students.txt";
string sss = string.Empty;
System.Collections.ArrayList ar = new System.Collections.ArrayList();
string[] lines = File.ReadAllLines(path).Where(arg => !string.IsNullOrWhiteSpace(arg)).ToArray();
string[] cols = lines[0]
.Trim()
.Split(new[] { '\t', ' ' }, StringSplitOptions.RemoveEmptyEntries);
int liness = 1;
foreach (string line in lines.Skip(1))
{
string[] cells = line
.Trim()
.Split(new[] { '\t' }, StringSplitOptions.RemoveEmptyEntries);
for (int counter = 0; counter < cols.Length; counter++)
{
string cellValue = "N/A";
if (counter < cells.Length)
cellValue = cells[counter];
Console.WriteLine("{0} {1}", cols[counter],cellValue);
if (cols[counter] == "ASC")
{
if (cellValue == "ASC10" || cellValue == "ASC20")
{
sss = "Flori";
}
//Here i have to check other column named "SAC" but HOWWWWWWWWWWWWWW??????????????????????????????????/ because processing is sequential
if (cellValue == "ASC30")
{
sss = "Texi";
}
}
}
liness++;
}
}
文本文件格式为
Firstname lastname ASC age salary location active SAC
Tom jerry ASC10 32 20000 NY Y 3
Sam peter ASC20 31 30000 KY N 4
jason sam ASC30 21 40000 JU Y 3
jerry Forman ASC20 34 23456 KK Y 4
要跳过非活动行,请获取 "active" 列的索引并在 foreach 循环开始时检查该值:
int indexActive = Array.indexOf(cols, "active");
if (indexActive >= 0 && indexActive < cells.Count() && cells[indexActive] == "N")
{
continue; // this skips this round of the foreach loop and continues with the next one
}
获取 SAC 列的索引并检查当前行中具有该索引的单元格,如下所示(在您的评论处插入):
int indexSAC = Array.indexOf(cols, "SAC");
if (indexSAC >= 0 && indexSAC < cells.Count())
{
if (cells[indexSAC] == "3")
{
sss = "Texi";
}
else if (cells[indexSAC] == "4")
{
sss = "Flori";
}
}
第一行也可以在您定义 cols
数组之后插入,这样您只需要执行一次。