【发布时间】:2017-09-26 15:13:25
【问题描述】:
我在创建最后一个文件时遇到问题。
我有一个制表符分隔的文本文件,看起来像这样。
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
基本上它是以单词 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 = "";
}
}
提前致谢
托马斯
【问题讨论】:
-
这不是制表符分隔的文件。那是一个包含复杂记录的文件。您需要编写一个解析器来了解每条记录何时开始以及如何处理每一行。你不能在一个循环中做到这一点。您应该编写可以识别每种类型的行的函数/类,例如,如果它以 KABEL 开头,则为 Header,如果以 PART 开头,则为 PART。之后每个函数识别自己的字段要容易得多,例如 PART 只需检查 3 个字段
-
顺便说一句,有一些工具可以让您创建像 ANTLR 或 FParsec 这样的解析器。您无需为每种类型的记录编写“识别器”,而是使用语法规则。
标签: c# parsing writealltext