读取包含换行符的CSV

时间:2013-01-07 10:15:57

标签: c#

我想将CSV读取到数组,但是csv在单元格中加入了换行符。

CSV(csvdata)

Title,Description,Tags,Category,Private,Images
MyGreatTitle1,"After this line is blankline
Text after blankline",techno,Tech,FALSE,C:\blogpostimg\img1.jpg
MyGreatTitle2,"After this line is blankline
Text after blankline",techno,Tech,FALSE,C:\blogpostimg\img2.jpg
MyGreatTitle3,"After this line is blankline
Text after blankline",techno,Tech,FALSE,C:\blogpostimg\img3.jpg
MyGreatTitle4,"After this line is blankline
Text after blankline",techno,Tech,FALSE,C:\blogpostimg\img4.jpg
MyGreatTitle5,"After this line is blankline
Text after blankline",techno,Tech,FALSE,C:\blogpostimg\img5.jpg
MyGreatTitle6,"After this line is blankline
Text after blankline",techno,Tech,FALSE,C:\blogpostimg\img6.jpg

我使用此代码:

string dir = AppDomain.CurrentDomain.BaseDirectory + @"blogpost";
string[] allLines = File.ReadAllLines(dir + "csvdatabase.csv");

如何逐行读取csv但不在单元格内读取?

1 个答案:

答案 0 :(得分:2)

正如卷云所说,您应该使用专用的CSV库,但如果您想自己动手(或了解如何操作),这里有一个快速编写的CSV解析器,可以给您一个想法。它不处理完整的CSV标准,只处理您的特定要求!

public class CsvParser
{
    private readonly List<List<string>> entries = new List<List<string>>();
    private string currentEntry = "";
    private bool insideQuotation;

    /// <summary>
    ///   Returns all scanned entries.
    ///   Outer IEnumerable = rows,
    ///   inner IEnumerable = columns of the corresponding row.
    /// </summary>
    public IEnumerable<IEnumerable<string>> Entries
    {
        get { return entries; }
    }

    public void ScanNextLine(string line)
    {
        // At the beginning of the line
        if (!insideQuotation)
        {
            entries.Add(new List<string>());
        }

        // The characters of the line
        foreach (char c in line)
        {
            if (insideQuotation)
            {
                if (c == '"')
                {
                    insideQuotation = false;
                }
                else
                {
                    currentEntry += c;
                }
            }
            else if (c == ',')
            {
                entries[entries.Count - 1].Add(currentEntry);
                currentEntry = "";
            }
            else if (c == '"')
            {
                insideQuotation = true;
            }
            else
            {
                currentEntry += c;
            }
        }

        // At the end of the line
        if (!insideQuotation)
        {
            entries[entries.Count - 1].Add(currentEntry);
            currentEntry = "";
        }
        else
        {
            currentEntry += "\n";
        }
    }
}

internal class Program
{
    private static void Main(string[] args)
    {
        string dir = AppDomain.CurrentDomain.BaseDirectory + @"blogpost";
        string[] allLines = File.ReadAllLines(dir + "csvdatabase.csv");

        CsvParser parser = new CsvParser();
        foreach (string line in allLines )
        {
            parser.ScanNextLine(line);
        }
    }
}