Home > Blockchain >  How to import csv file which contains qoute signs and the delimiter is coma?
How to import csv file which contains qoute signs and the delimiter is coma?

Time:10-20

So i am having a problem importing a csv file, i want to make an object from the columns but i cant read in properly.

So the header line looks like this: Title,Year,Genre,Rating,Votes,Directors The data line looks like this: The Last of Us: Mass Effect 2,2010,"Action, Adventure, Drama",9.5,19961,Casey Hudson

The problem is that, i get the exception "Input string is not in correct form" I am using coma as delimiter, is there a way to make quotes as delimiters too? Also, what are in the qoutes belongs to the Genre attribute.

I am using this code as the CsvParser right now:

using Games.Models;
using System.Globalization;
using System.Text;

namespace Games.Utils
{
    public class CsvParser
    {
        private readonly string _path;

        public char Delimiter { get; set; } = ',';
        public bool SkipFirst { get; set; } = true;
        public bool Verbose { get; set; } = true;
        public NumberFormatInfo NumberFormatInfo { get; private set; } = new NumberFormatInfo();
        public Encoding Encoding { get; set; } = Encoding.Default;

        public CsvParser(string path) => _path = path;

        public IEnumerable<Game> StreamParseGames() => GenerateGames(Enumerables.EnumerateStreamReaderLines(new(_path, Encoding)));

        public IEnumerable<Game> TextParseGames() => GenerateGames(File.ReadAllLines(_path, Encoding));

        private IEnumerable<Game> GenerateGames(IEnumerable<string> lineProvider)
        {
            if (SkipFirst) lineProvider = lineProvider.Skip(1);

            int lineNum = SkipFirst ? 1 : 0;
            foreach (var line in lineProvider)
            {
                string[] parts = line.Split(Delimiter);
                Game game;
                try
                {
                    game = new()
                    {
                        Title = parts[0],
                        Year = Convert.ToInt32(parts[1], NumberFormatInfo),
                        Genre = parts[2],
                        Rating = Convert.ToDouble(parts[3], NumberFormatInfo),
                        Votes = Convert.ToDouble(parts[4], NumberFormatInfo),
                        Directors = parts[5],
                    };
                }
                catch (FormatException e)
                {
                    if (Verbose) Console.WriteLine($"Line {lineNum   1:000000} omitted due: {e.Message}");
                    continue;
                }
                catch (IndexOutOfRangeException e)
                {
                    if (Verbose) Console.WriteLine($"Line {lineNum   1:000000} omitted due: {e.Message}");
                    continue;
                }
                finally
                {
                      lineNum;
                }
                yield return game;
            }
        }
    }
}

CodePudding user response:

I'd suggest you use CsvHelper which can deal with that instead of rolling your own CSV parser.

using CsvHelper;
using CsvHelper.Configuration;

var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
    Delimiter = ",",
};

using (var reader = new StreamReader("path\\to\\file.csv"))
using (var csv = new CsvReader(reader, config))
{
    var records = csv.GetRecords<Foo>();
}
  • Related