0

I have a csv file which has columns with /n characters and also /r/n and the column is contained in quotes something like this - "This is something/r/n Something/n Anything"

Example Data (copy paste this in a notepad and save it as .csv)

Note - please use escape and enter (/r/n) after This is Description and only enter after Something I am unable to include that in this editor.) -

INC2289067,"This is Description Something Anotherthing",2020-14-07 01:14:00,2020-16-07 09:35:00,CORP - CAD L2,GIS AP PC Support,4 - Low,Solved (Permanently),FALSE,2

I have tried this code -

 reader.onload = () => {
      let csvData = reader.result;
      let csvRecordsArray = (<string>csvData).split(/\r\n/);
}

and this is how my csvRecordsArray is split because of /r/n after "This is Description.." in 2nd column -

0: "INC2289067,\"This is Description"
1: "Something\nAnotherthing\",2020-14-07 01:14:00,2020-16-07 09:35:00,CORP - CAD L2,GIS AP PC Support,4 - Low,Solved (Permanently),FALSE,2"

Now how can i read this csv file without 3rd party libraries like NGXCSVParser without breaking my columns when there is an occurrence of /r/n or /n. NGXCSVParser does a great job but I cannot use it in my project because of compatibility issues cause we are using Angular 6. please help.

mebunnu
  • 115
  • 1
  • 10
  • You have to look for balanced quotation marks rather than simple splitting. Third party libraries have figured out how to do all that. You are not limited to ones that have "NG" in the name, you know. There are thousands out there that will take a string and parse it. – Heretic Monkey Jul 09 '21 at 13:20
  • @HereticMonkey how did i miss it? thank you so much for sharing the answer. – mebunnu Jul 10 '21 at 03:01

1 Answers1

0

You can simply use regex like:

("This is Description\r\nSomething\nAnotherthing").split(/\r?\n/);

console.log(("This is Description\r\nSomething\nAnotherthing").split(/\r?\n/))
Nitika
  • 424
  • 2
  • 9
  • This doesn't work with csv but works fine when it's alone. I still get line break when the csv is read with let csvRecordsArray = (csvData).split(/\r?\n/); – mebunnu Jul 10 '21 at 02:06