3

I have very big CSV with 244 columns and 4000 rows. There are a lot of \n\r, so when I try to split it with this (to find the end of a line) I get around 9000 rows instead of my wished 4000.

So how to determine which \n\r is within text or maybe at the end of a cell - and which is a definitive end of a line?

PassionateDeveloper
  • 14,558
  • 34
  • 107
  • 176

2 Answers2

2

When CSV file has data in column which is either \n,\r or , around these values usually put quotes. To correctly prase CSV I would recommend already existing parsers. See this answer as example.

If you truly want to be on your own you have to write simple state machine which will read data by individual columns. When reading column you have to take care about escaping rules. Only that way you could distinguish between line endings in data and line endings which separate rows

Community
  • 1
  • 1
codevision
  • 5,165
  • 38
  • 50
1

try using Environment.NewLine for splitting instead of \n\r

string path = yourfilepath;
string csv = System.IO.File.ReadAllText(path);
List<string> rows = csv.Split(new string[] {Environment.NewLine }, System.StringSplitOptions.RemoveEmptyEntries).ToList();
Chaitanya Gadkari
  • 2,669
  • 4
  • 30
  • 54