【问题标题】:Merge 2 lines in .CSV file using StreamReader使用 StreamReader 合并 .CSV 文件中的 2 行
【发布时间】:2014-12-12 10:02:59
【问题描述】:

我目前正在尝试合并 .csv 文件中的一些行。该文件遵循由“,”分隔的特定格式,最后一个元素使用 \n ascii 代码。这意味着最后一个元素被放到一个新行上,我返回一个只有一个元素的数组。我希望将此元素与其上方的行合并。

所以我的台词是:

192.168.60.24, ACD_test1,86.33352, 07/12/2014 13:33:13, False, Annotated, True,"Attribute1
Attribute 2
Attribute 3"
192.168.60.24, ACD_test1,87.33352, 07/12/2014 13:33:13, False, Annotated, True

是否可以将新的行属性与上面的行合并/加入?

我的代码如下所示:

var reader = new StreamReader(File.OpenRead(@path));
                string line1 = reader.ReadLine();
                if (line1.Contains("Server, Tagname, Value, Timestamp, Questionable, Annotated, Substituted"))
                {
                    while (!reader.EndOfStream)
                    {
                        List<string> listPointValue = new List<string>();
                        var line = reader.ReadLine();
                        var values = line.Split(',');

                        if (values.Count() < 2)
                        {
                            //*****Trying to Add Attribute to listPointValue.ElememtAt(0) here******
                        }
                        else
                        {
                            foreach (string value in values)
                            {
                            listPointValue.Add(value);
                            }
                            allValues.Add(listPointValue);
                        }
                    }
                   // allValues.RemoveAt(0);
                    return allValues;
                }

【问题讨论】:

    标签: c# csv streamreader


    【解决方案1】:

    我认为您想在执行 allValues.Add 之前阅读下一行。这样,您可以决定是否将前一行添加到 allValues(开始新行)。这让你明白我的意思:

    var reader = new StreamReader(File.OpenRead(@path));
    string line1 = reader.ReadLine();
    if (line1.Contains("Server, Tagname, Value, Timestamp, Questionable, Annotated, Substituted"))
    {
        List<string> listPointValue = new List<string>();
    
        // Add first line to listPointValue
       var line = reader.ReadLine();
       var values = line.Split(',');
       foreach (string value in values)
       {
            listPointValue.Add(value);
       }
    
       while (!reader.EndOfStream)
       {
            // Read next line
            line = reader.ReadLine();
            values = line.Split(',');
    
            // If next line is a full line, add the previous line and create a new line
            if (values.Count() > 1)
            {
                allValues.Add(listPointValue);
                listPointValue = new List<string>();
            }
    
            // Add values to line
            foreach (string value in values)
            {
                 listPointValue.Add(value);
            }
        }
        allValues.Add(listPointValue);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-01
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多