We've now made an updated video that addresses some of your questions about handling semicolon delimiters and CSV files with no header row, check it out! ua-cam.com/video/f5bzNh7hVhk/v-deo.html
Hey, thanks for this video. It was really informative and helped me out/solved my issue where my csv had a mixture of comma only and comma with quoted data eg 22, 5 , "abc", 6 , "def" . Thanks Great video :)
How can I deal with column name changes? If over time column names change and maybe their order. Or some are removed. Then I need some backwards compatibility between old reports in my app and new reports with the ever changing new format. When I download different formats that must map to the same class. 1. If the column header is dynamic in name then I can use regex. 2. If some columns are removed in the future I can use nullable properties.
Great video, quick question, where can I find the CSV used for this example?, Rocket launches I think is called, sorry for the trouble, and keep up the good work!👍🏼
Hi Robert, This content is owesome. Thanks for your help. I have one query: Currently my import code using MemoryStream and this CsvReader, to import CSV file. It is fine for small size file but when it reaches 40 MB the application crashes. I m using blazor tech. Someone said that import it in small batches one by one. But I don't know how. Can u please help me.
Hi Robert, nice videos you have here. Im using CSVHelper and ran into a problem. How can I make it read a string like this (This "is" a string). CSVHelper is breaking when getting double quotes in a string and i can't even handle anything because im only passing the file path. Thanks
This video helped me. I am learning C#. So as a project I have taken on to help me do this, I am trying to make an app that can take any datalog type CSV file, and plot the data on a livechart cartesian chart. But one thing i am trying to figure out, I want to be able to load any csv file with any number of columns. how can I create a dynamic class that can accommodate this?
Really happy it was helpful to you! If you use the dynamic keyword when calling GetRecords (e.g. GetRecords()) the library sorts out creating the dynamic objects for you. So for example if you had a CSV file with a column called "quantity", you'd get an object back that would have a quantity property on. The issue with using dynamics is that CsvHelper doesn't know what type the property should be, so just gives you a string, which you'd have to parse manually.
I have files that header names are not consistent. Sometimes an extra '_' or year is added anywhere. PrepareHeaderForMatch = args => args.Header.Replace("_", " "), but it doesn't work. even if I just set the header value to lowercase and then when I read csv headers it still shows me the original column names of the file. Any idea what am doing wrong here ?
Hi Roberts, I'm with a problem. I have a csv file with simple information, 1 column with a Name, 1 column with a Adress and 1 column with 1 ModbusFuction Name. I did like your tutorial, indeed I added the csv file to the directory of my visual studio project. I need to use this information forward in the project, so it's important to read correctly all the data so I can compare the names and adresses so I can do something, adding the information to a list is simple and effective, the problem is that one exception is launched when I run the program: "header with name 'Name'[0] was not found." .. I imagine that is because csv files begin in index 1 right ? Is there any command or solution so I can discard the reading of the index 0 ? Or some command that will start the reading at index 1 ? I was reading the documentation of the CsvHelper and I did not find any concrete solution. I thought that the Mapping solved this problem.
Great video! Very direct to the point and easy to follow. Only one question: Is there a place where I can download this CSV file you're using? I cheked the description of the video and couldn't find it.
Sure. Instead of .Name(...) in your map (or the [Name(...)] attribute), use Index(n) with the zero based index of the column See joshclose.github.io/CsvHelper/examples/configuration/class-maps/mapping-by-index/
Hey Robert, One more thing I want to asked that, my CSV file is updating in every 10 second..i.e. new data added into in in every 10 second..so is this code applicable for that..to retrieve particular data from file
Is the file itself updated or a new file created? As far as I know StreamReader will only give you file state at the time it is called (not tested it though). Might be worth having a look at the FileSystemWatcher class and subscribe to updates on the file? docs.microsoft.com/en-us/dotnet/api/system.io.filesystemwatcher?view=net-5.0
I've used powershell for years and im just now starting to try and learn other languages. It honestly baffles me how difficult it is to do such simple things like writing to a file or importing a csv. Powershell has cmdlets for everything and all the real complexities are abstracted away. I'm finding learning another language to be really difficult. Especially since i have no teacher and im just learning little bits and pieces from youtube and stackoverflow.
The irony being some of those cmdlets are probably written with .NET 😀 A good friend of mine does a lot of Python development and it always amazes me how easy it is to perform tasks like CSV processing, PDF generation, image processing etc in Python, when a lot of that is missing from .NET, or only available as commercial libraries
Hello Roberts Dev Talk, What if all of our fields of the CSV are separated by ";" how can we get rid of it to create the database? I mean something like id;contract;phoneNumber; ... and the data is 2;332321;7541233; ... P.S I really like the video.
When you construct your CsvReader you can pass in a CsvConfiguration object instead of CultureInfo. CsvConfiguration has a Delimiter property, which you can set to “;” For example var config = new CsvConfiguration(CultureInfo.InvariantCulture) config.Delimiter = “;” using (var reader = new CsvReader(streamreader, config)) Glad you liked the video!
its showing error" You must call read on the reader before accessing its data." like this ,and which data type should i use i want to read the data that is in japanese language
I'm rather new to the .Net scene, so my question is a bit easy to resolve. - I was wondering if we can rely on a console app and insert it into an API to run the code while receiving requests from an online interface (or webpage). would that be possible?
Hi Matt, do you mean have someone upload a CSV from a webpage, and then have a console app process the CSV and post to an API? Console app is a bit short lived and not really suited - you might want to do something with Worker Services, these are background processes designed to run over the long term. You could put your CSV into a queue from the API and then process from the worker service. I've made a playlist on background services in .NET ua-cam.com/play/PLGWG_rRY_j4MOABOppTH8XHJe8aOTyx2V.html
Hi Robert, your video is informative. Can you please tell me which version of visual code you are using and what other dlls you installed and can I try with CSOM to update the records to sharepoint list. Your information is appreciated 😊
Hi Durga. Thank you for the kind comment. Just to check, are you wanting to see an example of a CSV import to SharePoint using CSOM? If so, we can add that video to the upcoming schedule? For the CSV example here we used VS Code with the out of the box .NET core and installed the CSVHelper library (dotnet add package CSVHelper)
@@RDT thanks for the quick response Robert 😊 Yes I want it specifically for sharepoint list. I’m trying to create list items based on the data present in the csv file using C# CSOM console application Please help me if possible !
Hi, do you mean duplicate column names? You could try using indexes instead joshclose.github.io/CsvHelper/examples/configuration/class-maps/mapping-duplicate-names/
@@RDT hi sorry. No. I thought I meant headers, but what I meant was rows of unnecessary stuff until it reaches the headers. I.e. row 1 and 2 have random symbols and numbers .... row 3 starts the headers. I was hoping I could skip those unnecessary rows and just start at headers. Doesn't look like I can
I see, good question! There are some useful ideas here possibly? github.com/JoshClose/CsvHelper/issues/890 … but I haven’t tried it yet. I’m on vacation but will have a look when I get back and maybe make a follow up video as I can see a few questions building up 🙂
Hi, any idea how to handle this error when trying to add the CsvHelper package? error: There are no versions available for the package 'CsvHelper'. Using Visual Studio Code and SDK net5.0
what if you dont have column names . how will you determine headernames can u tell us little solution please. I believe it has something to do with CsvConfiguration but I am not sure
If you don't have column names you can use column index instead. So you set HasHeaderRecord in CsvConfiguration to false, and then instead of using .Name(..) on the Map function or in your class attributes, you just use Index(n) instead. So for example in a ClassMap: Map(m => m.Column1).Index(0) Or using attributes: [Index(0)] public string Column1 { get; set; } Hope that helps!
I'm still learning the ins and outs of Blazor - hope to make some videos on it soon. But IBrowserFile has an OpenReadStream method on it that you may be able to plug into a StreamReader/TextReader for the CsvReader constructor Source: docs.microsoft.com/en-us/aspnet/core/blazor/file-uploads?view=aspnetcore-5.0#file-streams docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.components.forms.ibrowserfile.openreadstream?view=aspnetcore-5.0
I cant make it work with a MemoryStream =( using (var MemoryStreamReader = new StreamReader(memoryStream, System.Text.Encoding.UTF8, true)) { var wholeText = MemoryStreamReader.ReadToEnd(); using (var csvReader = new CsvReader(MemoryStreamReader, CultureInfo.InvariantCulture)) { var records = csvReader.GetRecords().ToList(); _logger.LogInformation($"Finished downloading file [rom [{remoteFilePath}]"); } }
Hi rohit, CSV files are just plain text so theres no conversion needed (other than changing the file extension) - what sort of conversion were you thinking of?
@@RDT Thanks for your reply. Yes I do have. It's a type of file that the values are separated by ""the value"" ; ""the value""; something like that , I can send a copy of that file to you indeed.
We've now made an updated video that addresses some of your questions about handling semicolon delimiters and CSV files with no header row, check it out! ua-cam.com/video/f5bzNh7hVhk/v-deo.html
Keep making videos! As simple as CSV readers seem to be this concept has eluded me. Until now... You've got a new subscriber.
Thanks Mitchell, its always nice to know it has helped someone! We have lots of new content planned. Appreciate the sub!
The best I've seen!
Just FLAWLESS!
Thank you!
Wow, thank you! 🙏
This has been truly educational and useful. Thank you so much for sharing!
This was way more concise than anything google threw at me!
Thank you very much!
You’re welcome ☺️ glad it was helpful to you
Thank you so much sir! Finally understood how to use that lib! You're talented to teach us, clear didactic.
Brilliant. Thank you. Such a good teacher!!
Glad it was helpful 🙏
Thank you very much for the tip, we from Brazil are grateful for your video.
Our pleasure!
Hey, thanks for this video. It was really informative and helped me out/solved my issue where my csv had a mixture of comma only and comma with quoted data eg 22, 5 , "abc", 6 , "def" . Thanks Great video :)
wonderful video, Love from Pakistan !
This is so helpful many thanks mate!
You're welcome!
Loved one thanks.it very helpful for me
Thanks for your kind comment and support of the channel
Thanks. Video was easy to understand and got me going.
This video helped me a lot! You've got a new subscriber.
Happy to hear it was helpful for you. Thanks for the sub!
Thank you for the clear explanation! Do you know how I can change the csv path to the downloads folder
Thank you, very helpful video! Can you tell me what extension do you use to preview csv files in vscode?
well done. delivered very clearly and easy to follow. cheers, mate!
No problem!
Need to get this csv thing working !! Thanks
great Tutorial!
How can I deal with column name changes? If over time column names change and maybe their order. Or some are removed. Then I need some backwards compatibility between old reports in my app and new reports with the ever changing new format. When I download different formats that must map to the same class. 1. If the column header is dynamic in name then I can use regex. 2. If some columns are removed in the future I can use nullable properties.
Great video. What if comma embedded in the one of the column.
thank you for the tutorial! it helps a lot.
You're welcome!
Great tutorial! If I need some help with this, can I request your assistance?
great quality!
Thanks Matejoo , much appreciated
Great video, quick question, where can I find the CSV used for this example?, Rocket launches I think is called, sorry for the trouble, and keep up the good work!👍🏼
Hi Rosa, sure - it’s at my GitHub repo github.com/chrismroberts/csvreader-example
Hi Robert, This content is owesome. Thanks for your help.
I have one query: Currently my import code using MemoryStream and this CsvReader, to import CSV file. It is fine for small size file but when it reaches 40 MB the application crashes. I m using blazor tech. Someone said that import it in small batches one by one. But I don't know how. Can u please help me.
Hi Robert, nice videos you have here. Im using CSVHelper and ran into a problem. How can I make it read a string like this (This "is" a string). CSVHelper is breaking when getting double quotes in a string and i can't even handle anything because im only passing the file path. Thanks
Thanks for this good tutorial.
Sure! 🙏
This video helped me.
I am learning C#. So as a project I have taken on to help me do this, I am trying to make an app that can take any datalog type CSV file, and plot the data on a livechart cartesian chart. But one thing i am trying to figure out, I want to be able to load any csv file with any number of columns. how can I create a dynamic class that can accommodate this?
Really happy it was helpful to you! If you use the dynamic keyword when calling GetRecords (e.g. GetRecords()) the library sorts out creating the dynamic objects for you. So for example if you had a CSV file with a column called "quantity", you'd get an object back that would have a quantity property on. The issue with using dynamics is that CsvHelper doesn't know what type the property should be, so just gives you a string, which you'd have to parse manually.
I have files that header names are not consistent. Sometimes an extra '_' or year is added anywhere. PrepareHeaderForMatch = args => args.Header.Replace("_", " "), but it doesn't work. even if I just set the header value to lowercase and then when I read csv headers it still shows me the original column names of the file. Any idea what am doing wrong here ?
Hi Roberts,
I'm with a problem. I have a csv file with simple information, 1 column with a Name, 1 column with a Adress and 1 column with 1 ModbusFuction Name. I did like your tutorial, indeed I added the csv file to the directory of my visual studio project. I need to use this information forward in the project, so it's important to read correctly all the data so I can compare the names and adresses so I can do something, adding the information to a list is simple and effective, the problem is that one exception is launched when I run the program: "header with name 'Name'[0] was not found." .. I imagine that is because csv files begin in index 1 right ? Is there any command or solution so I can discard the reading of the index 0 ? Or some command that will start the reading at index 1 ? I was reading the documentation of the CsvHelper and I did not find any concrete solution.
I thought that the Mapping solved this problem.
Great video! Very direct to the point and easy to follow. Only one question: Is there a place where I can download this CSV file you're using? I cheked the description of the video and couldn't find it.
Thanks for watching, glad it was helpful to you 🙂 The CSV files I used and source code are here: github.com/chrismroberts/csvreader-example
Hi my register is ;...;"name "mr" robot"; ...; double quote is erros. How do I adjust to not give an error with this double quote?
Great tutorial, thanks! Can you tell, please, how convert "$2.34" to "2.34"( cut currency sign)?
Could use a regex to match the number part and then use Double.Parse on the resulting string?
Hey..can you suggest what will be the changes in code if my csv file have no header
Sure. Instead of .Name(...) in your map (or the [Name(...)] attribute), use Index(n) with the zero based index of the column
See joshclose.github.io/CsvHelper/examples/configuration/class-maps/mapping-by-index/
@@RDT Thank you so much 😊
Hey Robert, One more thing I want to asked that, my CSV file is updating in every 10 second..i.e. new data added into in in every 10 second..so is this code applicable for that..to retrieve particular data from file
Is the file itself updated or a new file created? As far as I know StreamReader will only give you file state at the time it is called (not tested it though). Might be worth having a look at the FileSystemWatcher class and subscribe to updates on the file? docs.microsoft.com/en-us/dotnet/api/system.io.filesystemwatcher?view=net-5.0
@@RDT Got it 👍 Thank you
I've used powershell for years and im just now starting to try and learn other languages. It honestly baffles me how difficult it is to do such simple things like writing to a file or importing a csv. Powershell has cmdlets for everything and all the real complexities are abstracted away. I'm finding learning another language to be really difficult. Especially since i have no teacher and im just learning little bits and pieces from youtube and stackoverflow.
The irony being some of those cmdlets are probably written with .NET 😀 A good friend of mine does a lot of Python development and it always amazes me how easy it is to perform tasks like CSV processing, PDF generation, image processing etc in Python, when a lot of that is missing from .NET, or only available as commercial libraries
Hello
Roberts Dev Talk,
What if all of our fields of the CSV are separated by ";" how can we get rid of it to create the database?
I mean something like id;contract;phoneNumber; ... and the data is 2;332321;7541233; ...
P.S I really like the video.
When you construct your CsvReader you can pass in a CsvConfiguration object instead of CultureInfo. CsvConfiguration has a Delimiter property, which you can set to “;”
For example
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
config.Delimiter = “;”
using (var reader = new CsvReader(streamreader, config))
Glad you liked the video!
@@RDT thank you so much, I got this problem and this code solved it
HI! Great talk, BUT do you have the software someplace so I can download it? Trying to copy it while you talk is very hard. Thanks! JIM
Hey JIM, here you go: github.com/chrismroberts/csvreader-example/
How can you read csv with duplicate headers using dynamic class in
Duplicate column names? You could try using indexes instead. joshclose.github.io/CsvHelper/examples/configuration/class-maps/mapping-duplicate-names/
its showing error" You must call read on the reader before accessing its data." like this ,and which data type should i use i want to read the data that is in japanese language
I'm rather new to the .Net scene, so my question is a bit easy to resolve. - I was wondering if we can rely on a console app and insert it into an API to run the code while receiving requests from an online interface (or webpage). would that be possible?
Hi Matt, do you mean have someone upload a CSV from a webpage, and then have a console app process the CSV and post to an API? Console app is a bit short lived and not really suited - you might want to do something with Worker Services, these are background processes designed to run over the long term. You could put your CSV into a queue from the API and then process from the worker service. I've made a playlist on background services in .NET ua-cam.com/play/PLGWG_rRY_j4MOABOppTH8XHJe8aOTyx2V.html
Hi Robert, your video is informative.
Can you please tell me which version of visual code you are using and what other dlls you installed and can I try with CSOM to update the records to sharepoint list.
Your information is appreciated 😊
Hi Durga. Thank you for the kind comment. Just to check, are you wanting to see an example of a CSV import to SharePoint using CSOM? If so, we can add that video to the upcoming schedule? For the CSV example here we used VS Code with the out of the box .NET core and installed the CSVHelper library (dotnet add package CSVHelper)
@@RDT thanks for the quick response Robert 😊
Yes I want it specifically for sharepoint list.
I’m trying to create list items based on the data present in the csv file using C# CSOM console application
Please help me if possible !
Does anyone know how to skip headers? I have two headers that's causing issues with the CsVReader..
Hi, do you mean duplicate column names? You could try using indexes instead joshclose.github.io/CsvHelper/examples/configuration/class-maps/mapping-duplicate-names/
@@RDT hi sorry. No. I thought I meant headers, but what I meant was rows of unnecessary stuff until it reaches the headers. I.e. row 1 and 2 have random symbols and numbers .... row 3 starts the headers. I was hoping I could skip those unnecessary rows and just start at headers. Doesn't look like I can
I see, good question! There are some useful ideas here possibly? github.com/JoshClose/CsvHelper/issues/890 … but I haven’t tried it yet. I’m on vacation but will have a look when I get back and maybe make a follow up video as I can see a few questions building up 🙂
Peng music 👍🏻
thanks
Robert, is there a way to work with positional data in text files instead of comma separators?
Hi Weslei, do you mean tab separated?
@@RDT I think he means fixed position: e.g. col1 1-5 col2 6-16 col3 17-25 etc
Hi, any idea how to handle this error when trying to add the CsvHelper package?
error: There are no versions available for the package 'CsvHelper'.
Using Visual Studio Code and SDK net5.0
great video by the way, love the way it is laid out and presented
Hmm, thats strange.. have you tried specifying a package version? I've just tried with a net5.0 project and it pulled version 27.1.1
Thank you!
what if you dont have column names . how will you determine headernames can u tell us little solution please. I believe it has something to do with CsvConfiguration but I am not sure
i dont know specifically about your case, but i think in csvhelper documentation site the map thing was used
If you don't have column names you can use column index instead. So you set HasHeaderRecord in CsvConfiguration to false, and then instead of using .Name(..) on the Map function or in your class attributes, you just use Index(n) instead.
So for example in a ClassMap:
Map(m => m.Column1).Index(0)
Or using attributes:
[Index(0)]
public string Column1 { get; set; }
Hope that helps!
@@RDT Thank you sir. it was timesaving respone :D
how do you do this with blazor 5.0 with file upload?
I'm still learning the ins and outs of Blazor - hope to make some videos on it soon. But IBrowserFile has an OpenReadStream method on it that you may be able to plug into a StreamReader/TextReader for the CsvReader constructor
Source:
docs.microsoft.com/en-us/aspnet/core/blazor/file-uploads?view=aspnetcore-5.0#file-streams
docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.components.forms.ibrowserfile.openreadstream?view=aspnetcore-5.0
I cant make it work with a MemoryStream =(
using (var MemoryStreamReader = new StreamReader(memoryStream, System.Text.Encoding.UTF8, true))
{
var wholeText = MemoryStreamReader.ReadToEnd();
using (var csvReader = new CsvReader(MemoryStreamReader, CultureInfo.InvariantCulture))
{
var records = csvReader.GetRecords().ToList();
_logger.LogInformation($"Finished downloading file [rom [{remoteFilePath}]");
}
}
How to convert a CSV file into TXT file using c#..
Pls help
Hi rohit, CSV files are just plain text so theres no conversion needed (other than changing the file extension) - what sort of conversion were you thinking of?
@@RDT please contact asap
Stucked in a project
Help
rohitkumarrairkr@gmail.com
I love you
awesome video.
Could you please try to convert csv files that have double quotes inside.
Thanks
Thanks for your question Salem, do you have an example of the kind of file you mean? Perhaps we can cover in a future video
@@RDT Thanks for your reply. Yes I do have. It's a type of file that the values are separated by ""the value"" ; ""the value""; something like that , I can send a copy of that file to you indeed.
Sure! robertsdevtalk@gmail.com
@@RDT Already sent!
Great tutorial, thank you very much!