-2

I want to ignore , in CSV File

and I want to read the data over 10

To do that I have to make a parcsCSVLine function

But I don't know how to make

private string[] parscCSVLine(string line)
    {
       //have to make this function
       return line.Split(',');
    }


    private List<List<string>> MakeRowbasedDataStructure()
    {
        StreamReader sr = new StreamReader(new FileStream("Market.csv", FileMode.Open), Encoding.Default);

        var line = sr.ReadLine();

        List<List<string>> data = new List<List<string>>();



        while(sr.EndOfStream==false)
        {
            line = sr.ReadLine();
            var values = parscCSVLine(line);
       
            data.Add(values.ToList());
          

        }

        sr.Close();
        return data;
        
    }
  • If you are using the code from the `parseCSVLine` method, then it returns a string array using `line.Split(‘,’);`… therefore… _”How to ignore the comma (,)”_ ?… you can’t… it is not possible to ignore the comma “,” when using the standard .Net `Split` method. There may be some other third-party string split method to do this or roll your own. My question is if you want to ignore the comma, then why are you splitting on a comma? – JohnG Oct 18 '20 at 06:21
  • What's wrong with your implementation? To make your question clearer, add some sample input data and the expected result. – Klaus Gütter Oct 18 '20 at 06:22
  • So many CSV parsers out there, and they're so simple to use; why are you reinventing the wheel? For example [How to use CsvHelper by Josh Close](https://joshclose.github.io/CsvHelper/getting-started) or another one.. [How to use Csv by Steve Hansen](https://github.com/stevehansen/csv/) – Caius Jard Oct 18 '20 at 06:35
  • Does this answer your question? [Reading CSV files using C#](https://stackoverflow.com/questions/3507498/reading-csv-files-using-c-sharp) – Hoshani Oct 18 '20 at 07:55

2 Answers2

1

I guess you're asking "how to tell Split not to split if the comma is data, not a delimiter"

You can't; string.Split will just see this:

John,Doe,"100 The Avenue,Cityville,State",30,True

And split it to:

John
Doe
"100 The Avenue
Cityville
State"
30
True

You'll have to write your own parsing for it;

Either do something that works char by char with a logic of

  • if char is a quote flip Boolean insideString (Initial value: false) to opposite state
  • if char is a comma and insidestring is false, substring from last comma to this one

Or

  • use string.Split, then
  • turn the result into a list
  • loop through the split results and if one starts with a comma,
    • stat another loop that appends the next ones onto it (with a comma) until you reach one that ends in a quote..

In this latter method you rejoin the fields that Split dumbly split


But as mentioned in the comments, why bother? Csv parsing has been done to death by other people: just use one of those libraries.. you can use TextFieldParser or even the access database driver if you want a Microsoft solution/no third party packages

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
0

you can just do something like this:

 var splitedLine = new List<string>();

 splitedLine = line.Split(",").ToList();

There is no need for the while loop.

But as stated before in the comments, try using an already working library for this.

Here is a more extensive tutorial: https://dotnetcoretutorials.com/2018/08/04/csv-parsing-in-net-core/

Ozadaka
  • 5
  • 4