How to split csv whose columns may contain ,

前端 未结 8 1162
我寻月下人不归
我寻月下人不归 2020-11-22 06:35

Given

2,1016,7/31/2008 14:22,Geoff Dalgas,6/5/2011 22:21,http://stackoverflow.com,"Corvallis, OR",7679,351,81,b437f461b3fd27387c5d8ab47a293d35,34

相关标签:
8条回答
  • 2020-11-22 07:25

    It is so much late but this can be helpful for someone. We can use RegEx as bellow.

    Regex CSVParser = new Regex(",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))");
    String[] Fields = CSVParser.Split(Test);
    
    0 讨论(0)
  • 2020-11-22 07:31

    It is a tricky matter to parse .csv files when the .csv file could be either comma separated strings, comma separated quoted strings, or a chaotic combination of the two. The solution I came up with allows for any of the three possibilities.

    I created a method, ParseCsvRow() which returns an array from a csv string. I first deal with double quotes in the string by splitting the string on double quotes into an array called quotesArray. Quoted string .csv files are only valid if there is an even number of double quotes. Double quotes in a column value should be replaced with a pair of double quotes (This is Excel's approach). As long as the .csv file meets these requirements, you can expect the delimiter commas to appear only outside of pairs of double quotes. Commas inside of pairs of double quotes are part of the column value and should be ignored when splitting the .csv into an array.

    My method will test for commas outside of double quote pairs by looking only at even indexes of the quotesArray. It also removes double quotes from the start and end of column values.

        public static string[] ParseCsvRow(string csvrow)
        {
            const string obscureCharacter = "ᖳ";
            if (csvrow.Contains(obscureCharacter)) throw new Exception("Error: csv row may not contain the " + obscureCharacter + " character");
    
            var unicodeSeparatedString = "";
    
            var quotesArray = csvrow.Split('"');  // Split string on double quote character
            if (quotesArray.Length > 1)
            {
                for (var i = 0; i < quotesArray.Length; i++)
                {
                    // CSV must use double quotes to represent a quote inside a quoted cell
                    // Quotes must be paired up
                    // Test if a comma lays outside a pair of quotes.  If so, replace the comma with an obscure unicode character
                    if (Math.Round(Math.Round((decimal) i/2)*2) == i)
                    {
                        var s = quotesArray[i].Trim();
                        switch (s)
                        {
                            case ",":
                                quotesArray[i] = obscureCharacter;  // Change quoted comma seperated string to quoted "obscure character" seperated string
                                break;
                        }
                    }
                    // Build string and Replace quotes where quotes were expected.
                    unicodeSeparatedString += (i > 0 ? "\"" : "") + quotesArray[i].Trim();
                }
            }
            else
            {
                // String does not have any pairs of double quotes.  It should be safe to just replace the commas with the obscure character
                unicodeSeparatedString = csvrow.Replace(",", obscureCharacter);
            }
    
            var csvRowArray = unicodeSeparatedString.Split(obscureCharacter[0]); 
    
            for (var i = 0; i < csvRowArray.Length; i++)
            {
                var s = csvRowArray[i].Trim();
                if (s.StartsWith("\"") && s.EndsWith("\""))
                {
                    csvRowArray[i] = s.Length > 2 ? s.Substring(1, s.Length - 2) : "";  // Remove start and end quotes.
                }
            }
    
            return csvRowArray;
        }
    

    One downside of my approach is the way I temporarily replace delimiter commas with an obscure unicode character. This character needs to be so obscure, it would never show up in your .csv file. You may want to put more handling around this.

    0 讨论(0)
提交回复
热议问题