How do I handle line breaks in a CSV file using C#?

前端 未结 14 1776
情书的邮戳
情书的邮戳 2020-12-15 08:43

I have an Excel spreadsheet being converted into a CSV file in C#, but am having a problem dealing with line breaks. For instance:

\"John\",\"23\",\"555-555         


        
相关标签:
14条回答
  • Rather than check if the current line is missing the (") as the first character, check instead to see if the last character is a ("). If it is not, you know you have a line break, and you can read the next line and merge it together.

    I am assuming your example data was accurate - fields were wrapped in quotes. If quotes might not delimit a text field (or new-lines are somehow found in non-text data), then all bets are off!

    0 讨论(0)
  • 2020-12-15 09:42

    A somewhat simple regular expression could be used on each line. When it matches, you process each field from the match. When it doesn't find a match, you skip that line.

    The regular expression could look something like this.

    Match match = Regex.Match(line, @"^(?:,?(?<q>['"](?<field>.*?\k'q')|(?<field>[^,]*))+$");
    if (match.Success)
    {
      foreach (var capture in match.Groups["field"].Captures)
      {
        string fieldValue = capture.Value;
        // Use the value.
      }
    }
    
    0 讨论(0)
提交回复
热议问题