我有一个文本文件
maker no. = 178 05/03/18 11:55:23
ROD WTg SIZEmmL OVALmm PDmm
1 0.740 23.94 0.28 357
2 0.751 23.98 0.29 368
3 0.733 23.95 0.39 351
4 0.747 23.99 0.32 363
5 0.734 23.96 0.23 356
6 0.742 23.89 0.64 365
7 0.745 23.98 0.39 366
8 0.739 23.93 0.29 361
9 0.745 23.98 0.39 366
10 0.739 23.93 0.29 361
n = 10
WTg SIZEmmL OVALmm PDmm
MEAN 0.7411 23.946 0.364 361.1
SD 0.0060 0.037 0.115 5.2
MIN 0.733 23.88 0.23 351
MAX 0.751 23.99 0.64 368
11:09:36
********************************************************************************
我想做的是按列阅读此文本文件,并将每一列存储到单独的Array中。然后将它们写入另一个文件。该输出文件应该像
<DATA SampleID="1" WTg="0.740" SIZEmmL ="23.94" OVALmm ="0.28" />
<DATA SampleID="2" WTg="0.751" SIZEmmL ="23.98" OVALmm ="0.29" />
<DATA SampleID="3" WTg="0.733" SIZEmmL ="23.95" OVALmm "0.39" />
我也想像上面显示的那样打印另一列。 到目前为止,我所做的是
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
using (StreamReader reader = new StreamReader(File.OpenRead("D:\\ProcessFolderFile.txt")))
{
using (StreamWriter writer = new StreamWriter(File.Open("D:\\ResultFile.txt", FileMode.Create)))
{
string line;
while ((line = reader.ReadLine()) != null)
{
string[] columns = line.Split(' ');
WriteAndPrint(writer, "<DATA>");
WriteAndPrint(writer, columns[1]);
WriteAndPrint(writer, "</DATA>");
}
}
}
Console.ReadLine();
}
static void WriteAndPrint(StreamWriter writer, string line)
{
Console.WriteLine(line);
writer.WriteLine(line);
}
}
}
但是它不会像我期望的那样打印
答案 0 :(得分:1)
首先,您必须过滤数据。
您似乎很幸运,相关行都以数字开头,其他所有行都没有。
while ((line = reader.ReadLine()) != null)
{
string[] columns = line.Split(' ');
if(columns.Length != 5) continue;
if (! int.TryParse(cxolums[0])) continue;
// now you know you have 5 columns, at positions 0..4
}