I am new to this type of coding.I need your help to solve my issue.
I have a .csv file with some data and am able to extract data but, am facing issue with some comma separated fields with in csv file.
Example I have employeefulname as "LastName,FirstName",but when I read this am getting as two separated fields.I don't want split emplloyeefullname,I want what exact it is getting.
Example I have employeefulname as "LastName,FirstName",but when I read this am getting as two separated fields.I don't want split emplloyeefullname,I want what exact it is getting.
I assume you wrote code that explicitly splits by comma. Commonly, CSV files that have fields containing a delimiter characters are defined within double quotes.
"Lastname, Firstname", "Next field"
Otherwise you need to write code to concatenate the fields at some point in the process. Since you have not posted your code or file layout we can't provide an accurate solution. However, this is relatively simple logic to figure out. if you are reading
the file line by line and splitting by a comma, then the full name would be in two array elements. You cna put then together again using concatenation.
items[0] + ", " + item[1]
Where zero and one are the indexes of the last and first name within the array.
Otherwise, post your code and any errors that you are receiving.
Add a reference to Microsoft.VisualBasic and a using statement for Microsoft.VisualBasic.FileIO. It does not matter if you are using C#, as in the sample below. That the beauty of the .Net CLR!
This way you have the option to set HasFieldsEnclosedInQuotes=true;, which will eliminate your problem.
Mark all posts that give the desired result the answer. If you only mark the last that gave you clarification because you misread an earlier post others will be confused. Some of us are here to help others and our point to post ratio matters.
Member
1 Points
165 Posts
Parse CSV data and mapping to my database fields
Jun 12, 2017 07:21 AM|mdr.devender|LINK
Hi All,
I am new to this type of coding.I need your help to solve my issue.
I have a .csv file with some data and am able to extract data but, am facing issue with some comma separated fields with in csv file.
Example I have employeefulname as "LastName,FirstName",but when I read this am getting as two separated fields.I don't want split emplloyeefullname,I want what exact it is getting.
I surf in net,but I didn't get correct solution.
Please help me to solve my issue.
Thanks in advance.
All-Star
53731 Points
24057 Posts
Re: Parse CSV data and mapping to my database fields
Jun 12, 2017 01:48 PM|mgebhard|LINK
I assume you wrote code that explicitly splits by comma. Commonly, CSV files that have fields containing a delimiter characters are defined within double quotes.
Otherwise you need to write code to concatenate the fields at some point in the process. Since you have not posted your code or file layout we can't provide an accurate solution. However, this is relatively simple logic to figure out. if you are reading the file line by line and splitting by a comma, then the full name would be in two array elements. You cna put then together again using concatenation.
Where zero and one are the indexes of the last and first name within the array.
Otherwise, post your code and any errors that you are receiving.
Participant
1380 Points
608 Posts
Re: Parse CSV data and mapping to my database fields
Jun 12, 2017 02:01 PM|JBetancourt|LINK
you better use this library:
http://joshclose.github.io/CsvHelper/
but if you want to do it manually (when running the split function) use as separator "," instead of just ,
that will work if all your fields are delimited by double quotes
Please remember to click "Mark as Answer" the responsES that resolved your issue.
Contributor
7058 Points
2189 Posts
Re: Parse CSV data and mapping to my database fields
Jun 12, 2017 02:22 PM|ryanbesko|LINK
Add a reference to Microsoft.VisualBasic and a using statement for Microsoft.VisualBasic.FileIO. It does not matter if you are using C#, as in the sample below. That the beauty of the .Net CLR!
https://forums.asp.net/post/6143670.aspx
This way you have the option to set HasFieldsEnclosedInQuotes = true;, which will eliminate your problem.