文本文件包含记录之间的回车符

时间:2018-11-29 16:30:13

标签: c# asp.net

我有一个文本文件,其中包含以分隔格式显示的表格数据。 记录之间有几行发生换行。 有什么原因可以解决这个问题

例如:

Date,S.No,Comments

2018-11-10,1,This is line one

2018-11-10,2,this is 

line two

2018-11-10,3,this is line 

three

在上面的第二和第三数据行示例中,在到达行尾并创建新行之前发生了换行。

1 个答案:

答案 0 :(得分:1)

这是我对您的问题发表评论的想法。

using System;
using System.Collections.Generic;

namespace _53543524_FileWithBrokenRecords
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> records = new List<string>();
            using (System.IO.StreamReader sr = new System.IO.StreamReader("M:\\StackOverflowQuestionsAndAnswers\\53543524_FileWithBrokenRecords\\sampledata.txt"))
            {
                string currentLine = string.Empty;
                bool headerline = true;
                System.Text.RegularExpressions.Regex newRecordSartRegex = new System.Text.RegularExpressions.Regex("^\\d{4}-\\d{2}-\\d{2},");
                while ((currentLine = sr.ReadLine()) != null)
                {
                    if (headerline)
                    {
                        //this handles the header record
                        records.Add(currentLine);
                        headerline = false;
                    }
                    else if (newRecordSartRegex.IsMatch(currentLine))
                    {
                        //this is a new record, so create a new entry in the list
                        records.Add(currentLine);
                    }
                    else
                    {
                        //this is the continuation on a ongoing record,
                        //so append to the last item in the list
                        if (!string.IsNullOrWhiteSpace(currentLine))
                        {
                            //add to the current record only if the line is not empty
                            records[records.Count - 1] += currentLine;
                        }
                    }
                }
            }

            foreach (string item in records)
            {
                Console.WriteLine(item);
            }
            Console.ReadLine();
        }
    }
}

结果是这样的:

Date,S.No,Comments
2018-11-10,1,This is line one
2018-11-10,2,this is line two
2018-11-10,3,this is line three