【问题标题】:Read a Text File skipping Lines that has a TAB Character in them读取文本文件跳过其中包含 TAB 字符的行
【发布时间】:2013-12-18 15:09:59
【问题描述】:

你好!我对 C# 完全陌生,需要知道如何逐行读取文件(跳过其中包含制表符的文件)并将文本设置为 RichTextBox! 我一直在这样做:

DialogResult result = open_dialog.ShowDialog();

if (result == DialogResult.OK)
{
    string file_name = open_dialog.FileName;
    System.IO.StreamReader sr = new System.IO.StreamReader(file_name);
    String data;

    data = sr.ReadToEnd();
    rich_words.Text = data;

    String line;

    using (var file = System.IO.File.OpenText(fileName))
    {
        // read each line, ensuring not null (EOF)
        while ((line = file.ReadLine()) != null)
        {
            if(line.Contains('\t'))
                //do nothing
            else
                richbox.Text=line;
        }
    }
}

但我没有这样做!很伤心,因为我已经尽力了,现在我似乎什么也没做! 我的样本数据是:

生命体征

                respirations    
           vital signs  
           vital sign checks    
           vital signs/blood pressure   
               blood pressure observation
               blood pressure
               auscultate blood pressures
               monitor blood pressure
               check blood pressure
               blood pressure gauge

监控

           monitor skin color   
           monitor characteristics  
           monitor pulse    
           monitor presence 
           monitor  
           monitor/monitor temperature  
               monitor temperature
               continuous temperature monitoring device

检查

           check body temperature   
           check frequency  
           check pain level 
           special checks   
           check    
           check body temperaturewith   

上面突出显示的文本有制表符,所以我不需要它们。请为此提出任何解决方案。我只需要像标题这样的词:|生命体征| |监视器|和 |检查|等等...有人可以帮我解决这个问题吗?

【问题讨论】:

  • 除非您发布的代码与您的实际代码不同,否则您的富文本框只会显示不包含选项卡的最后一行:richbox.Text = line; 您需要附加 富文本框的每一行
  • 不,它根本不工作! :(

标签: c# delimiter csv


【解决方案1】:
DialogResult result = open_dialog.ShowDialog();
if (result == DialogResult.OK) {
    StreamReader sr = new StreamReader(open_dialog.FileName);

    StringBuilder() sb = new StringBuilder();

    // Don't set the rich_words.Text = data here because there's no need to.

    string line;
    using (var file = File.OpenText(dialog.FileName)) {
        while ((line == file.ReadLine()) != null) {
            if (!line.Contains('\t')) {
                sb.AppendLine(line);
            }
            // No need to have an else since we only want to do stuff when the line does not contain a tab.
        }
    }

    // Now that you have all of the text from the file into your StringBuilder, you add it as the text in the box.
    rickbox.Text = sb.ToString();
}

您所做的是每次一行不包含制表符时更改文本框中的文本。您正在覆盖所有内容。

【讨论】:

    【解决方案2】:

    krillgar 的回答已经解释了为什么您的示例不起作用,但这里是您的所有代码作为一个衬里,只是为了好玩:

    richbox.Text = String.Join(Environment.NewLine, File.ReadAllLines(file).Where(l => !l.Contains("\t")).ToArray());
    

    【讨论】:

    • 完全同意。然而,正如他们说他们是 C# 的新手并且他们的方法中根本没有其他 LINQ,我不想混淆 OP。
    【解决方案3】:

    你可以使用这种方法

    if(Char.IsControl(line[0]))
      //do nothing
    else
      richbox.Text=line;
    

    【讨论】:

    • 这仍然会不断地用读取的最后一行覆盖richbox中的文本,而不是文件中的所有所需数据。
    • 我看过。但我不知道为什么user3115728接受了我的回答。
    • 是的,我也对同样的事情感到好奇。当 OP 做了这样没有意义的事情时,也许我们应该向网站推荐“拒绝接受的答案”功能。您至少应该获得一个徽章,因为您接受了具有负面评价的答案。 ;)
    • 一顶帽子。这绝对应该是一顶帽子。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-22
    • 1970-01-01
    • 2012-11-21
    • 1970-01-01
    相关资源
    最近更新 更多