【问题标题】:How to perform text matching in text file and then move that next line after text into csv file?如何在文本文件中执行文本匹配,然后将文本后的下一行移动到 csv 文件中?
【发布时间】:2016-10-26 04:48:04
【问题描述】:

我需要读取文本文件,然后检查文本文件内容中的输入文本。

如果存在输入文本,则将下一行(在输入文本之后)移动到 csv 文件中。

示例输入: input.txt

Hi

Hello

-------------------------------------------------------------------------------
Code Name       ID Customers     CID  Time.    %   
==================== ========= =========== ============ ===== ================= 

Harish SM        1001    Tower      India      44.58  
Siva DM          2310    Cata       China      56.78  



No Name           ID       Customers        
==================== ========= =========== 
MS Norway         1001     UNIBIC   

Datas are inside the csv file.
THanks

如果我输入“代码名称”,那么代码名称后面的 2 行将保存在 csv 文件中,直到换行。

如果输入“No Name”,那么 No Name 之后的一行将存储在另一个 csv 文件中。

只需要用c#编写代码。

我对 c# 控制台应用程序有点了解

如何编写代码来执行文本文件读取和内容到 csv?

【问题讨论】:

  • 你的问题很不清楚。您想从文本文件中读取数据并根据代号从 CSV 中查找更多信息吗?后面两排是什么意思?
  • 在“代号”下方有两行以“Harish SM& Siva DM”开头的行移到 csv 文件中
  • 你能展示你的尝试吗?
  • 我刚刚尝试了代码,但我不知道如何使用逻辑读取匹配的“代码名称”的下一行。

标签: c# csv text


【解决方案1】:

由于输入文件的格式不是最好的设置,我们需要进行预读以使其正常工作。以下过程应该可以将给定 ID 代码之后的行提取到字符串数组中,第一个包含标识符行。

public string[] GetDataLinesFromFile(string filename, string searchString)
    {
        List<string> dataEntries = new List<string>();

        using (System.IO.StreamReader stream = new System.IO.StreamReader(filename))
        {
            System.IO.TextReader tr = stream;

            bool foundSearchString = false;
            string lastLine = string.Empty;
            string line = string.Empty;

            while (!stream.EndOfStream)
            {
                lastLine = line;
                line = tr.ReadLine();
                if (lastLine.Trim().StartsWith(searchString) && line.Contains("===================="))
                {
                    foundSearchString = true;
                    continue;
                }
                if (foundSearchString)
                {
                    // Start after the divider line
                    if (lastLine.Contains("===================="))
                        continue;

                    // If the current line read is a marker line, then our last line is actually a new identifier line
                    if (line.Contains("===================="))
                    {
                        // Can be used to look for multiple listings with the same ID
                        foundSearchString = false;
                        continue;

                        // If you only want the first found ID, uncomment this and comment out above
                        // return dataEntries.ToArray();
                    }

                    // If our previously read line is not empty, add it to the list of strings
                    if (lastLine.Trim().Length != 0)
                        dataEntries.Add(lastLine);
                }
            }
        }
        return dataEntries.ToArray();

    }

然后要使用这个函数,就这样调用它:

string[] entries = GetDataLinesFromFile("input.txt", "Code Name");

要将这些保存到 .csv 文件中,您只需遍历字符串数组中的每个字符串并查找制表符标记作为分隔符,或者如果列处于设定宽度,那么您将硬编码这些宽度在。

【讨论】:

  • 如果我输入“No Name”,那么结果可能只显示“MS Norway”行。但它也显示“数据在 csv 中,谢谢”,并添加了 MS 挪威
  • 不幸的是,由于文件的设计,这无济于事。在解析时之前放置另一个 ================ 行
  • 不幸的是,由于文件的设计,这无济于事。在空白行之后和末尾的文本之前放置另一个 ================ 行将纠正此问题。另一种选择是在解析 string[] 时,如果您检测到不符合规范的行(不包含与第一行相同数量的制表符),那么您可以丢弃它以及任何剩余的行。跨度>
  • 您能根据我的情况编辑代码吗?
  • 上面发布的文件的布局没有显示它是使用制表符作为分隔符还是仅使用空格。另外,我假设由于最初用于显示选择的字体,列的对齐方式不完整。如果每一行的数据由 TAB 字符分隔,那么这很容易在 string[] -> .csv 函数中完成。否则你需要确定列宽
猜你喜欢
  • 2020-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多