The BEST Way to Read a CSV File in C# | CsvHelper Tutorial

  Рет қаралды 59,188

Roberts Dev Talk

Roberts Dev Talk

3 жыл бұрын

In this tutorial I'll show you how to read a CSV file into your C# .NET app using the excellent CsvHelper library. I'll go through 3 methods to read your CSV file: dynamic objects, Property Attributes and ClassMap.
The example here is a .NET Core app, but CsvHelper is a .NET Standard library, so it works seamlessly across .NET Core, .NET Framework and Xamarin.
☕️ Don’t forget, developers run on coffee! 😀
www.buymeacoffee.com/Robertsd...
📬 Stay in touch! Join our mailing list
robertsdevtalk.com
🐦 Follow us on Twitter:
/ robertsdevtalk
If you need to create a CSV file (export), we have a video here for you:
• Create CSV file in C# ...
CsvHelper homepage:
joshclose.github.io/CsvHelper/
SpaceX open source REST API (unofficial), lots of fun sample data if you like rockets:
github.com/r-spacex

Пікірлер: 104
@RDT
@RDT 3 жыл бұрын
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! kzfaq.info/get/bejne/nJuSrbGZnM66mJ8.html
@fellypsantos_
@fellypsantos_ Жыл бұрын
Thank you so much sir! Finally understood how to use that lib! You're talented to teach us, clear didactic.
@paulkalus41
@paulkalus41 2 жыл бұрын
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 :)
@DA_7
@DA_7 7 ай бұрын
This has been truly educational and useful. Thank you so much for sharing!
@skyhymitch
@skyhymitch 3 жыл бұрын
Keep making videos! As simple as CSV readers seem to be this concept has eluded me. Until now... You've got a new subscriber.
@RDT
@RDT 3 жыл бұрын
Thanks Mitchell, its always nice to know it has helped someone! We have lots of new content planned. Appreciate the sub!
@patrickboehme4556
@patrickboehme4556 2 жыл бұрын
Great tutorial, thank you very much!
@briantremelling
@briantremelling 2 жыл бұрын
Thanks. Video was easy to understand and got me going.
@bigoper
@bigoper Жыл бұрын
The best I've seen! Just FLAWLESS! Thank you!
@RDT
@RDT Жыл бұрын
Wow, thank you! 🙏
@rmarikar1
@rmarikar1 2 жыл бұрын
Brilliant. Thank you. Such a good teacher!!
@RDT
@RDT 2 жыл бұрын
Glad it was helpful 🙏
@Terrados1337
@Terrados1337 Жыл бұрын
This was way more concise than anything google threw at me! Thank you very much!
@RDT
@RDT Жыл бұрын
You’re welcome ☺️ glad it was helpful to you
@ayesamson
@ayesamson 2 жыл бұрын
well done. delivered very clearly and easy to follow. cheers, mate!
@RDT
@RDT 2 жыл бұрын
No problem!
@Sabafbatlett
@Sabafbatlett 8 ай бұрын
This is so helpful many thanks mate!
@RDT
@RDT 8 ай бұрын
You're welcome!
@alexdpaiva83
@alexdpaiva83 2 жыл бұрын
Thank you very much for the tip, we from Brazil are grateful for your video.
@RDT
@RDT 2 жыл бұрын
Our pleasure!
@davidgorenflo9260
@davidgorenflo9260 Жыл бұрын
great Tutorial!
@Koltayy
@Koltayy 2 жыл бұрын
thank you for the tutorial! it helps a lot.
@RDT
@RDT 2 жыл бұрын
You're welcome!
@glooman2455
@glooman2455 2 жыл бұрын
This video helped me a lot! You've got a new subscriber.
@RDT
@RDT 2 жыл бұрын
Happy to hear it was helpful for you. Thanks for the sub!
@Amy-ve3ke
@Amy-ve3ke 2 жыл бұрын
Need to get this csv thing working !! Thanks
@IAmRaj360
@IAmRaj360 2 жыл бұрын
Loved one thanks.it very helpful for me
@RDT
@RDT Жыл бұрын
Thanks for your kind comment and support of the channel
@TheMassijay
@TheMassijay Жыл бұрын
Thank you, very helpful video! Can you tell me what extension do you use to preview csv files in vscode?
@hassanOnYoutube
@hassanOnYoutube 2 жыл бұрын
wonderful video, Love from Pakistan !
@AlbrechtJ
@AlbrechtJ 3 жыл бұрын
Thanks for this good tutorial.
@RDT
@RDT 3 жыл бұрын
Sure! 🙏
@matejoo7663
@matejoo7663 3 жыл бұрын
great quality!
@RDT
@RDT 3 жыл бұрын
Thanks Matejoo , much appreciated
@alialsaady5
@alialsaady5 Жыл бұрын
Thank you for the clear explanation! Do you know how I can change the csv path to the downloads folder
@miguelvale7364
@miguelvale7364 2 жыл бұрын
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
@Matzul98
@Matzul98 11 ай бұрын
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.
@DeepakSharma-wi4wu
@DeepakSharma-wi4wu 2 жыл бұрын
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 ?
@udayshastri5288
@udayshastri5288 Жыл бұрын
Great video. What if comma embedded in the one of the column.
@robertosousa8065
@robertosousa8065 2 жыл бұрын
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.
@santoshanjani3860
@santoshanjani3860 Жыл бұрын
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.
@cifuentes678
@cifuentes678 2 жыл бұрын
Great tutorial! If I need some help with this, can I request your assistance?
@SrDividendos
@SrDividendos 2 жыл бұрын
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.
@RDT
@RDT 2 жыл бұрын
Thanks for watching, glad it was helpful to you 🙂 The CSV files I used and source code are here: github.com/chrismroberts/csvreader-example
@Gmaaa
@Gmaaa Жыл бұрын
Hi my register is ;...;"name "mr" robot"; ...; double quote is erros. How do I adjust to not give an error with this double quote?
@ILucas24I
@ILucas24I Жыл бұрын
I love you
@saeedahamed9347
@saeedahamed9347 Жыл бұрын
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
@clsferguson
@clsferguson 3 жыл бұрын
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?
@RDT
@RDT 3 жыл бұрын
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.
@mateusloubach
@mateusloubach 2 жыл бұрын
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?
@RDT
@RDT 2 жыл бұрын
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 kzfaq.info/sun/PLGWG_rRY_j4MOABOppTH8XHJe8aOTyx2V
@durgaagoni8988
@durgaagoni8988 3 жыл бұрын
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 😊
@RDT
@RDT 3 жыл бұрын
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)
@durgaagoni8988
@durgaagoni8988 3 жыл бұрын
@@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 !
@rosabenitez7795
@rosabenitez7795 Жыл бұрын
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!👍🏼
@RDT
@RDT Жыл бұрын
Hi Rosa, sure - it’s at my GitHub repo github.com/chrismroberts/csvreader-example
@romanlemeshov5513
@romanlemeshov5513 3 жыл бұрын
Great tutorial, thanks! Can you tell, please, how convert "$2.34" to "2.34"( cut currency sign)?
@RDT
@RDT 3 жыл бұрын
Could use a regex to match the number part and then use Double.Parse on the resulting string?
@Michael-xb5zq
@Michael-xb5zq Жыл бұрын
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.
@RDT
@RDT Жыл бұрын
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
@WesleiGomesdeSousa
@WesleiGomesdeSousa 2 жыл бұрын
Robert, is there a way to work with positional data in text files instead of comma separators?
@RDT
@RDT 2 жыл бұрын
Hi Weslei, do you mean tab separated?
@getsmartpaul
@getsmartpaul 2 жыл бұрын
@@RDT I think he means fixed position: e.g. col1 1-5 col2 6-16 col3 17-25 etc
@friendlyfox2189
@friendlyfox2189 3 жыл бұрын
how do you do this with blazor 5.0 with file upload?
@RDT
@RDT 3 жыл бұрын
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
@chrismantonuk
@chrismantonuk 3 жыл бұрын
Peng music 👍🏻
@RDT
@RDT 3 жыл бұрын
thanks
@jimhat5753
@jimhat5753 2 жыл бұрын
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
@RDT
@RDT 2 жыл бұрын
Hey JIM, here you go: github.com/chrismroberts/csvreader-example/
@one-g4227
@one-g4227 2 жыл бұрын
How can you read csv with duplicate headers using dynamic class in
@RDT
@RDT 2 жыл бұрын
Duplicate column names? You could try using indexes instead. joshclose.github.io/CsvHelper/examples/configuration/class-maps/mapping-duplicate-names/
@mikeloose9270
@mikeloose9270 2 жыл бұрын
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
@mikeloose9270
@mikeloose9270 2 жыл бұрын
great video by the way, love the way it is laid out and presented
@RDT
@RDT 2 жыл бұрын
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
@RDT
@RDT 2 жыл бұрын
Thank you!
@grandmagus8618
@grandmagus8618 3 жыл бұрын
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
@raanabaniamerian428
@raanabaniamerian428 3 жыл бұрын
i dont know specifically about your case, but i think in csvhelper documentation site the map thing was used
@RDT
@RDT 3 жыл бұрын
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!
@grandmagus8618
@grandmagus8618 3 жыл бұрын
@@RDT Thank you sir. it was timesaving respone :D
@McSeeMo_
@McSeeMo_ 2 жыл бұрын
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}]"); } }
@irvirv8243
@irvirv8243 3 жыл бұрын
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.
@RDT
@RDT 3 жыл бұрын
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!
@savs4657
@savs4657 2 жыл бұрын
@@RDT thank you so much, I got this problem and this code solved it
@anushripatil1059
@anushripatil1059 3 жыл бұрын
Hey..can you suggest what will be the changes in code if my csv file have no header
@RDT
@RDT 3 жыл бұрын
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/
@anushripatil1059
@anushripatil1059 3 жыл бұрын
@@RDT Thank you so much 😊
@anushripatil1059
@anushripatil1059 3 жыл бұрын
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
@RDT
@RDT 3 жыл бұрын
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
@anushripatil1059
@anushripatil1059 3 жыл бұрын
@@RDT Got it 👍 Thank you
@ld8778
@ld8778 2 жыл бұрын
Does anyone know how to skip headers? I have two headers that's causing issues with the CsVReader..
@RDT
@RDT 2 жыл бұрын
Hi, do you mean duplicate column names? You could try using indexes instead joshclose.github.io/CsvHelper/examples/configuration/class-maps/mapping-duplicate-names/
@ld8778
@ld8778 2 жыл бұрын
@@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
@RDT
@RDT 2 жыл бұрын
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 🙂
@rohitrockzz2633
@rohitrockzz2633 3 жыл бұрын
How to convert a CSV file into TXT file using c#.. Pls help
@RDT
@RDT 3 жыл бұрын
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?
@rohitrockzz2633
@rohitrockzz2633 3 жыл бұрын
@@RDT please contact asap Stucked in a project Help rohitkumarrairkr@gmail.com
@salemal-khalify7205
@salemal-khalify7205 3 жыл бұрын
awesome video. Could you please try to convert csv files that have double quotes inside. Thanks
@RDT
@RDT 3 жыл бұрын
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
@salemal-khalify7205
@salemal-khalify7205 3 жыл бұрын
@@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.
@RDT
@RDT 3 жыл бұрын
Sure! robertsdevtalk@gmail.com
@salemal-khalify7205
@salemal-khalify7205 3 жыл бұрын
@@RDT Already sent!
Coding Gem #1.4: Parsing CSV Data as Objects in C#
14:33
ParametricCamp
Рет қаралды 12 М.
Turns out REST APIs weren't the answer (and that's OK!)
10:38
Dylan Beattie
Рет қаралды 128 М.
Nastya and SeanDoesMagic
00:16
Nastya
Рет қаралды 42 МЛН
Это реально работает?!
00:33
БРУНО
Рет қаралды 1,9 МЛН
World’s Largest Jello Pool
01:00
Mark Rober
Рет қаралды 109 МЛН
Create CSV file in C# the EASY way | CsvHelper Tutorial
7:14
Roberts Dev Talk
Рет қаралды 31 М.
Easy C# Tutorial - Understand Attributes and Write your Own!
13:24
Working With The File System in C# - Managing Folders and Files
35:07
What are record types in C# and how they ACTUALLY work
15:36
Nick Chapsas
Рет қаралды 119 М.
Brutally honest advice for new .NET Web Developers
7:19
Ed Andersen
Рет қаралды 118 М.
DHH discusses SQLite (and Stoicism)
54:00
Aaron Francis
Рет қаралды 57 М.
Coding Gem #1.3: Parsing CSV Data as Lists in C#
24:31
ParametricCamp
Рет қаралды 26 М.
Reading from a CSV File and Searching for a Record in C# (Simple)
8:13
PHP doesn't suck (anymore)
10:48
Aaron Francis
Рет қаралды 202 М.