-1

I am making a .CSV parser for my dad, because he is recieving an almost unreadable file from a place he buys wares at.

See, my problems are like this:

  1. The shop have been so kind to make the seperators "," and still using it to seperate decimal values.
  2. Everything is enclosed in "quotation marks", and i would love to know how to remove them

Until now i have been using StreamReader to read my file, and to show it i am using a DataGridView. My code are as this: http://dumptext.com/E9GcAfyW

EDIT: The problems are as this: When i try to parse the file with my current code, i get an error saying the array are too long for the specified columns, and thus crashing. That is because the .CSV file are using "," as the seperator, and still using the comma as a seperator for decimal values (e.g. 25,3). The other problem is because when i parse a chunk of the file, it works well, but all the elements are enclosed in quotation marks, which i would love to remove from the DataGridView. This is my window when i remove the extra comma so the parser works: http://puu.sh/fbXlt/35aaf37e33.png

  • You might find the [TextFieldParser class](https://msdn.microsoft.com/en-us/library/microsoft.visualbasic.fileio.textfieldparser%28v=vs.110%29.aspx) useful. From MSDN help: "The TextFieldParser object provides methods and properties for parsing structured text files". IMO this is a better option than attempting to parse the file using `StreamReader`. See [this SO answer](http://stackoverflow.com/a/20523165/4265041). – Steven Rands Jan 29 '15 at 16:03
  • What's your actual question? What specifically isn't working? I don't see anything immediately wrong with that CSV, what problems are you encountering? – David Jan 29 '15 at 16:03
  • Sounds like it might be a dump from an excel file. This [answer](http://stackoverflow.com/questions/4403194/split-using-delimiter-except-when-delimiter-is-escaped/4404096#4404096) to another question might help. I'd also suggest finding a library to do the parsing for you. – juharr Jan 29 '15 at 16:07
  • Side note: one thing is creating something useful (in this case you'd look to use existing tools/libraries) another is to learn something (than you try, show code that does not work,...) - but doing both at the same time is often dangerous and makes relationships worse as result... – Alexei Levenkov Jan 29 '15 at 16:09
  • Show us what you have so we can help solve your issues. The CSV file they are providing is well formed. So currently I am unsure what the problem is. – IdahoSixString Jan 29 '15 at 16:10
  • Johas Strand, I've removed unrelated text from your post. Please make sure edit did not lose important information and inline *small* sample of CSV and *small and relevant* part of the code into your post if you still need the question answered. – Alexei Levenkov Jan 29 '15 at 16:12

2 Answers2

0

I had a similar CSV file and I ended up using the TextFieldParser to extract the data. You will still need to parse the CSV Column Data:

private void GetCSVData(string CsvFileFullPath)
{
    // Make sure that you add the Microsoft.VisualBasic.FileIO Namespace

    using (var lines = new TextFieldParser(CsvFileFullPath))
    {
        lines.HasFieldsEnclosedInQuotes = true;
        lines.SetDelimiters(",");
        lines.TrimWhiteSpace = true;

        try
        {
            while (!lines.EndOfData)
            {
                string[] csvLineCols = lines.ReadFields();

                for (int i = 0; i < csvLineCols.Count(); i++)
                {
                    Console.WriteLine(csvLineCols[i].ToString());
                }
            }
        }
        catch
        { }
    }
}
Mark Kram
  • 5,672
  • 7
  • 51
  • 70
0

If you are going to need to bind this to a DataGrid is may be best just to parse it into a List. If you need the data to be type safe it will be up to you to figure out how to parse the data accordingly.

This example uses the NuGet package CsvHelper.

WebClient webClient = new WebClient();
TextReader textReader = new StreamReader(new MemoryStream(
                webClient.DownloadData("http://selfservice.diges.dk:9080/selfservice/download/prisbog/VARPOST.CSV")));
 var csvReader = new CsvParser(textReader);

 List<Record> records = new List<Record>();
 while (true)
 {
     var row = csvReader.Read();
     if (row == null)
     {
         break;
     }

     records.Add(new Record()
     {
         Column1 = row[0],
         Column2 = row[1],
         Column3 = row[2],
         Column4 = row[3],
         Column5 = row[4],
         Column6 = row[5],
         Column7 = row[6]
     });
 }

public class Record
{
    public string Column1 { get; set; }
    public string Column2 { get; set; }
    public string Column3 { get; set; }
    public string Column4 { get; set; }
    public string Column5 { get; set; }
    public string Column6 { get; set; }
    public string Column7 { get; set; }
}
IdahoSixString
  • 643
  • 10
  • 18