【问题标题】:Reading text data from tab delimited file without headers in C#在 C# 中从没有标题的制表符分隔文件中读取文本数据
【发布时间】:2023-01-01 20:35:49
【问题描述】:

我想从一个 .txt 文件中读取文本数据,其中的数据是制表符分隔. 我正在粘贴下面的示例数据。 [1]:https://i.stack.imgur.com/t9cPt.png

现在我想要的是将数据读入一个列表类型细绳 没有标题.

我目前有以下代码:

            
            var sepList = new List<string>();

            // Read the file and display it line by line.
            using (var file = new StreamReader(docPath))
            {
                string line;
                while ((line = file.ReadLine()) != null)
                {
                    var delimiters = '\t';
                    var segments = line.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);

                    foreach (var segment in segments)
                    {
                        //Console.WriteLine(segment);
                        sepList.Add(segment);
                    }   
                }
            }

任何帮助将不胜感激:)[![在此处输入图片描述][1]][1]

【问题讨论】:

    标签: c# filestream


    【解决方案1】:

    我想要的是将数据读入一个没有标题的字符串类型列表。

    在读取文件之前添加 file.ReadLine(); 它将跳过文件的标题或第一行。

    试试这个:-

    var sepList = new List<string>();
    
    
    using (var file = new StreamReader(docPath))
    {
        // Skip the first line (the header)
        file.ReadLine();
    
        string line;
        while ((line = file.ReadLine()) != null)
        {
            var delimiters = '	';
            var segments = line.Split(
               delimiters, StringSplitOptions.RemoveEmptyEntries
            );
    
            foreach (var segment in segments)
            {
                // console.WriteLine(segment);
                sepList.Add(segment);
            }   
        }
    }
    
    

    【讨论】:

      猜你喜欢
      • 2014-08-26
      • 2012-12-01
      • 1970-01-01
      • 2015-05-21
      • 2018-05-24
      • 1970-01-01
      • 2016-03-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多