我在创建最后一个文件时遇到问题。
我有一个制表符分隔的文本文件,看起来像这样。
KABEL Provkanna for Windchill_NWF-TSNM =2212.U001+++-X2 PXC.2400016 =2271.U004+++-X1 Test_Created_in_WT =2212-W123 RXF 4x25 0000000440 Cable RXF 4x25
PART 01 1 1
PART 02 2 2
PART 03 3 3
PART 04 4 4
PART SH GND GND
KABEL Provkanna for Windchill_NWF-TSNM =2212.U001+++-X2 PXC.2400016 =2271.U004+++-X1 Test_Created_in_WT =2212-W124 RXF 4x35 0000000456 Cable RXF 4x35
PART 01 1 5 5
PART 02 1 6 6
PART 03 1 7 7
PART 04 1 8 8
PART SH 1 GND GND
KABEL Provkanna for Windchill_NWF-TSNM =2212.U001+++-X2 PXC.2400016 =2271.U004+++-X1 Test_Created_in_WT =2212-W125 RXF 4x35 0000000456 Cable RXF 4x35
PART 01 1 9 9
PART 02 1 10 10
PART 03 1 11 11
PART 04 1 12 12
PART SH 1 GND GND
基本上它是以 Word KABEL 开头的一行,后跟许多制表符分隔的列。该行之后是一些以单词 PART 开头的行。以 PART 开头的行数可以不同。
现在我想把这个文件分解成几个文件。
每个已解析文件的名称都应包含来自以 KABEL 开头的行的某一列的信息。在该文件中,应添加以 PART 开头的每一行。
然后,当再次出现以 KABEL 开头的行时,将创建一个新文件,并将 PART 行添加到该文件中......等等......等等。
我来回尝试了很多,最终找到了一种正确创建前两个文件的方法......但是......最后一个文件不会被创建。
我的脚本读取并找到并显示应该是最后解析的输出文件的唯一部分的正确列,但我没有看到任何文件正在输出。
有接盘侠吗?我会非常感谢你的帮助,因为我被困住了......
{
string line ="";
string ColumnValue ="";
string Starttext = "PART";
string Kabeltext = "KABEL";
int column = 16;
string FilenameWithoutCabelNumber = @"C:\Users\tsnm2171\Desktop\processed\LABB\OUTPUT - Provkanna for Windchill_NWF-TSNM_2212_CABLE_CONNECTION";
string ExportfileIncCablenumber ="";
string filecontent ="";
using (System.IO.StreamReader reader = new System.IO.StreamReader(@"C:\Users\tsnm2171\Desktop\processed\LABB\Provkanna for Windchill_NWF-TSNM_2212_CABLE_CONNECTION.txt"))
{
line = reader.ReadLine();
//Set columninnehåll till filnamn (String ColumnValue)
string [] words = line.Split();
ColumnValue = words[column];
MessageBox.Show (ColumnValue);
while (line != null)
{
line = reader.ReadLine();
if (line.StartsWith(Kabeltext)) // if line starts with KABEL
{
ExportfileIncCablenumber = (FilenameWithoutCabelNumber + "-" + ColumnValue + ".txt");
System.IO.File.WriteAllText(ExportfileIncCablenumber, filecontent);
filecontent = string.Empty;
string [] words2 = line.Split();
ColumnValue = words2[column];
MessageBox.Show("Ny fil " + ColumnValue);
}
else if (line.StartsWith(Starttext)) // if line starts with PART
{
filecontent += ((line)+"\n"); //writes the active line
}
}
ExportfileIncCablenumber = (FilenameWithoutCabelNumber + "-" + ColumnValue + ".txt");
System.IO.File.WriteAllText(ExportfileIncCablenumber, filecontent); filecontent = "";
}
}
提前致谢
托马斯