0

我在创建最后一个文件时遇到问题。

我有一个制表符分隔的文本文件,看起来像这样。

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 = "";                                                                   
    }
}

提前致谢

托马斯

4

1 回答 1

0

首先,您应该像这样进行读取行和空值检查模式, while((line = reader.ReadLine()) != )因为它可以保护您免受空值引用。我的版本,这似乎工作:

{
        const string StartText                  = "PART";
        const string KabelText                  = "KABEL";  
        const string FilenameWithoutCabelNumber = @"...\";

        string fileContent = "";
        int    fileNumber  = 0;

        using (StreamReader reader = File.OpenText(@"...\file.txt"))
        {       
            string line = reader.ReadLine();
            string columnValue = GetParticularColumnName(line);
            //Set columninnehåll till filnamn (String ColumnValue)   
            MessageBox.Show (ColumnValue);

            var ExportfileIncCablenumber ="";
            while ((line = reader.ReadLine()) != null)         
            {   
                if (line.StartsWith(KabelText)) // if line starts with KABEL 
                {   
                    ExportfileIncCablenumber =  $"{FilenameWithoutCabelNumber}-{columnValue}({fileNumber}).txt";

                    File.WriteAllText(ExportfileIncCablenumber, fileContent);

                    fileContent = string.Empty;
                    columnValue = GetParticularColumnName(line);
                    fileNumber++;
                }
                else if (line.StartsWith(StartText)) // if line starts with PART
                {
                    fileContent += ((line)+Environment.NewLine);    //writes the active line                                
                }                   
            }

            ExportfileIncCablenumber =  (FilenameWithoutCabelNumber + "-" + columnValue + ".txt");
            File.WriteAllText(ExportfileIncCablenumber, fileContent);
        }
    }

    private static string GetParticularColumnName(string line)
    {
        return line.Split(' ').Last();
    }

您在保存文件时遇到的问题是因为对String.Split()工作方式的误解。有关详细信息,请参阅文档,但要简短:

如果 separator 参数为 null 或不包含任何字符,则该方法将空白字符视为分隔符。

这就是为什么你有一个包含单词和空字符串的数组。column正在选择空字符串,这就是为什么你有一个文件覆盖另一个文件。(列值 16 也是错误的,实际上有 15 个字)。您所有的行都被连接起来,因为 Windows 不会将 '\n' 视为结束行字符,这就是我使用的原因Environment.NewLine 最后但并非最不重要的问题是您的代码样式。确实,您应该遵守 .Net 的通用编码约定,因为这会使您的代码连贯且更具可读性。

于 2017-09-26T16:46:18.043 回答