Quotes in tab-delimited file

后端 未结 7 1967
我寻月下人不归
我寻月下人不归 2021-01-27 16:58

I\'ve got a simple application that opens a tab-delimited text file, and inserts that data into a database.

I\'m using this CSV reader to read the data: http://www.codep

7条回答
  •  抹茶落季
    2021-01-27 17:43

    I recently solved a similar issue, and although CsvReader was working properly on all but a few lines of my TSV file, what solved my problem in the end was setting a customDelimiter in the constructor of CsvReader

    public static void ParseTSV(string filepath)
        {
            using (CsvReader csvReader = new CsvReader(new StreamReader(filepath), true, '\t')) {
            //if that didn't work, passing unlikely characters into the other params might help
            //using (CsvReader csvReader = new CsvReader(new StreamReader(filepath), true, '\t', '~', '`', '~', ValueTrimmingOptions.None)) {
                int fieldcount = csvReader.FieldCount;
    
                //Does not work, since it's read only property
                //csvReader.Delimiter = "\t";
    
                string[] headers = csvReader.GetFieldHeaders();
    
                while (csvReader.ReadNextRecord()) {
                    for (int i = 0; i < fieldcount; i++) {
                        string msg = String.Format("{0}\r{1};", headers[i],
                                                   csvReader[i]);
                        Console.Write(msg);
                    }
                    Console.WriteLine();
                }
            }
        }
    

提交回复
热议问题