PassionateDeveloper
PassionateDeveloper

Reputation: 15138

CSV with \n\r in lines - how to define a line end?`

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?

Upvotes: 3

Views: 2735

Answers (2)

codevision
codevision

Reputation: 5520

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

Upvotes: 2

Chaitanya Gadkari
Chaitanya Gadkari

Reputation: 2787

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();

Upvotes: 1

Related Questions