Python Tutorial: CSV Module - How to Read, Parse, and Write CSV Files

Поделиться
HTML-код
  • Опубликовано: 12 май 2024
  • In this Python Programming Tutorial, we will be learning how to work with csv files using the csv module. We will learn how to read, parse, and write to csv files. CSV stands for "Comma-Separated Values". It is a common format for storing information. Knowing how to read, parse, and write this information to files will open the door to working with a lot of data throughout the world. Let's get started.
    The code from this video can be found at:
    github.com/CoreyMSchafer/code...
    ✅ Support My Channel Through Patreon:
    / coreyms
    ✅ Become a Channel Member:
    / @coreyms
    ✅ One-Time Contribution Through PayPal:
    goo.gl/649HFY
    ✅ Cryptocurrency Donations:
    Bitcoin Wallet - 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
    Ethereum Wallet - 0x151649418616068fB46C3598083817101d3bCD33
    Litecoin Wallet - MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot
    ✅ Corey's Public Amazon Wishlist
    a.co/inIyro1
    ✅ Equipment I Use and Books I Recommend:
    www.amazon.com/shop/coreyschafer
    ▶️ You Can Find Me On:
    My Website - coreyms.com/
    My Second Channel - / coreymschafer
    Facebook - / coreymschafer
    Twitter - / coreymschafer
    Instagram - / coreymschafer
    #Python

Комментарии • 742

  • @HellO-ly1bp
    @HellO-ly1bp 6 лет назад +769

    I've just interviewed for an amazing Python job, and if I get it I swear I will owe you so much. Everyone says this already but they're right, your videos are fantastic, thank you for all your time and effort!

    • @coreyms
      @coreyms  6 лет назад +139

      That's great, Nina! It makes my day to hear feedback like this. I wish you the best in getting the job. Good luck!

    • @HellO-ly1bp
      @HellO-ly1bp 6 лет назад +396

      So guess what... I got the job! Big big thanks again, it means so much to me to get a job like this. I'll be sure to keep on recommending your videos!

    • @syedabdulrahmans3357
      @syedabdulrahmans3357 6 лет назад +16

      Congrats Nina!

    • @HellO-ly1bp
      @HellO-ly1bp 6 лет назад +140

      I guess it's special to me because of the struggle it took to get here. My degree isn't in Computer Science, but I really wanted to get into programming and I thought it'd be too difficult to get my foot in the door. But the job I've just got is at a tier 1 investment bank as a Developer, so now I'll get to do something I enjoy for a job, get paid really well for it, and live/work in a beautiful part of London. Not a bad place to start!

    • @RockDavid
      @RockDavid 5 лет назад +6

      @@HellO-ly1bp Hey its been a year since you posted this. Im a CS well Computer ENgineer, and i learning this stuff for last 3 weeks goign for 3 months strait. I wanted to ask
      What did they ask about what you needed to know about Python and what are you constatnly doing at your job with Python? Like type of things your coding or editing?
      Thanks and its very late but grats on the job :)

  • @jedsonguedes
    @jedsonguedes 4 года назад +56

    I gotta say: it's so rare to find someone with such a great ability for teaching.
    You should do another one for XML parsing.

  • @vegito_blue6226
    @vegito_blue6226 5 лет назад +11

    Hi Corey, thanks for this video. For my interview tomorrow this is what I was looking for.

  • @jingyuchang1885
    @jingyuchang1885 5 лет назад +18

    I just realized whenever I have a question about python, as long as I come to your channel, I can find the answer! Thanks very much!

  • @Xaminn
    @Xaminn 4 года назад +28

    Every time I need help with a certain topic (and you've made a video on it), your video pretty much instantly solves my problem. I've spent an hour or so looking through forums and tutorials and this solved my issue in about 5 minutes. Thank you yet again for your expertise. 5/5

  • @melissamejianisonger3860
    @melissamejianisonger3860 6 лет назад +13

    I've spent a week trying to figure this out. I watched your video and it took me 10 minutes! THANK YOU. THANK YOU.

  • @Alex-ht1oq
    @Alex-ht1oq 6 лет назад +159

    You are so good at teaching man. I appreciate every video that you produce.

    • @ryaxn_a7495
      @ryaxn_a7495 6 лет назад

      nitty? oh, are you wondering why you have no friends? because when someone produces something absolutely amazing you don't notice it???

    • @shermanmerman4944
      @shermanmerman4944 6 лет назад +2

      it's a compliment. get over yourself.

    • @MyTube4Utoo
      @MyTube4Utoo 6 лет назад +1

      +JDI2002 Seriously, the guy pays Corey a much deserved compliment and you manage to find fault with it?

  • @ourcore
    @ourcore Год назад +1

    I hadn't touched Python in years and I've learnt so much from your videos about requests/requests-html, reading APIs, and CSV files. Thanks so much!

  • @devslaven
    @devslaven 5 лет назад +7

    Any time I'm hunting for information on Python, my searches always lead to you and I'm never disappointed. Keep up the great work!

  • @superliang2887
    @superliang2887 5 лет назад +23

    This video is the best, I watched many videos on RUclips, only this one really help

  • @raybilton6995
    @raybilton6995 6 лет назад +3

    Excellent series and I'm learning a lot from them. Your tutorials are my goto when I have a question. I really wish you had extended this video to include writing the two individual fields instead of just using delete. It would have helped with my needs more. I am working on a project with 20 fields in my input, but needing to write only 15 fields from the original file, and add two new fields to my 17 field output file.

  • @CuriousAnonDev
    @CuriousAnonDev Год назад +4

    Thank You sir, this lectures are still, in 2022, faaarrr more better than other so called tutorials
    It is difficult to believe someone gave such free high quality content years ago, you helped millions!!!! Thank You so so much

  • @praveen0227
    @praveen0227 6 лет назад +4

    I am a beginner in Python but whenever i watch your tutorials i feel its easy to write programs in python.The examples are very good. I wish if you can give some exercises based on the videos you show which will help beginners like me to write more programs.

  • @trying2understand870
    @trying2understand870 5 лет назад +3

    I really enjoyed this tutorial as well as all of your other tutorials that I have seen. I was wondering if you had shown a method for updating csv files?

  • @JV-jc7ci
    @JV-jc7ci Год назад

    You have a very, very simple teaching style that makes it easy for newbies to understand. You're a great teacher! Thank you for the awesome FREE tutorial! This was so much more useful than online courses I am paying for!

  • @NazachatGames
    @NazachatGames 2 года назад

    Corey, you make me have two interviews and real oportunities for a job, here in Argentina.
    Thanks you a lot.
    I'm now a member of your channel, hope u the best.

  • @scochran3265
    @scochran3265 4 года назад +7

    What I love seeing in the below comments is people actually helping other people. There isn’t any bashing or calling each other names. Lovely.

    • @coreyms
      @coreyms  4 года назад +4

      Yeah, when I first started making videos I was worried about the comments, but this community of people is fantastic. Super nice and helpful.

  • @tlrlutz
    @tlrlutz 3 года назад +64

    Love the tutorials Corey they help so much in learning Python. Just thought I would update this video because the syntax is off around 8:00. The context manager "with open('new_names', 'w') as new_file:" will return a CSV file with a blank row in-between each row. The quick fix is to add "with open('new_names', 'w', newline='') as new_file:" and the issue goes away.

    • @francescob1904
      @francescob1904 7 месяцев назад +1

      thanks you so much, I was wondering about what I did wrong...

    • @ugestacoolie5998
      @ugestacoolie5998 5 месяцев назад +1

      @@francescob1904yeah me too and I was wondering why there was an extra line after each original line XD

  • @vergilfrans1447
    @vergilfrans1447 4 года назад +1

    Coding after long time...Feeling excited! Thank you for your tutorial. Easy to follow.

  • @auditocanarsie7355
    @auditocanarsie7355 2 года назад +1

    You are a natural professor. I appreciate the clarity & completeness. The text is easy to read on a desktop but was a little hard for me to read on a cell phone. If there was a way to zoom in on the text so it could be easier to read on a cell phone that is the only thing that could possible improve this video. Either that or I just need to visit my Optometrist and your videos are flawless.😀

  • @kirupv
    @kirupv 5 лет назад +1

    You covered very well through explaining simple way. Thank you very much

  • @soumyazyx
    @soumyazyx 4 года назад +1

    Thank you Corey for your time and effort. Keep spreading the knowledge.

  • @shaunakalshi4547
    @shaunakalshi4547 4 года назад +1

    Thank you for the tutorial.Helped me a lot in my miniproject.

  • @luminamoon504
    @luminamoon504 2 года назад

    Total life saver, I needed this for a project due today, thank you so much!

  • @AK-kj8cc
    @AK-kj8cc 6 лет назад

    You are very thorough in your explanations. I've subbed and I hope you keep this up!

  • @GiovanniGirelli1104
    @GiovanniGirelli1104 4 года назад +4

    Hello Corey, I just discovered your channel today and it is awesome. Funny that you cover all the topics I am interested in (Python, SQL, Pandas, MatPlotlib etc). For people like me who never even came close to going to college/university this is awesome. One topic I would really like to see would be a tutorial on (if at all possible) automating the process of extracting tabular data from PDF's using Python and convert them into useful Excel files (something I need to do on a daily basis at work). Awesome channel, keep it up!

  • @aldrinmagnoa9874
    @aldrinmagnoa9874 3 года назад

    You make everything so simple to understand. thank you so much!

  • @valeriewang6244
    @valeriewang6244 5 лет назад +14

    OMG Corey you are soooooo helpfullllllllllll. I love you!

  • @elementalneil7967
    @elementalneil7967 2 года назад

    I was doing this Data Science course on Coursera, and the lesson on the csv module covered a load of content in a very short time. Of course, not understanding a thing, I had to revert back to RUclips. This video clears it up really well. Thanks.

  • @ronitchawla4937
    @ronitchawla4937 4 года назад +48

    I am taking Harvard University's online CS50 course and I could understand JACK about CSV file till I saw this vid 😂 .... Thanks a lot man 👍

  • @michaelpadovani9566
    @michaelpadovani9566 6 лет назад

    Hey thanks for posting this, I'm very new to Python (and watched a lot of your other tutorials). I'm a meteorologist that works with instrumentation datasets so i live and breathe HUGE csv files lol Hoping to use this method tutoring a local HS student for his project. Thx again and cheers from Maryland!

  • @cringeyonmain
    @cringeyonmain 2 месяца назад

    Saved my life when doing my higher computing science coursework. Thanks so much!

  • @connorpayne7324
    @connorpayne7324 Год назад

    very good, not only do you explain it very well, you explain each part of what you are doing and why. all in all a very good tutorial.

  • @sameerk12982
    @sameerk12982 6 лет назад +1

    Thank you very much Sir for creating such an easy to understand tutorial about reading writing csv file.

  • @MelanieCrypto
    @MelanieCrypto 2 года назад

    refreshingly thorough and helpful ! thanks for posting this

  • @QuarktaschemitSenf
    @QuarktaschemitSenf Год назад +1

    Thank you very much Corey. I switched job and now work with python. your videos helped a lot not to lose it in the beginning :-)

  • @hamidurrahman3183
    @hamidurrahman3183 6 лет назад +2

    Brother Corey Schafer, your parents should be proud of you. You are a genius. Great at explaining. I have watched almost all videos of this python series. They are blessing to every python beginner. Thanks for all those videos.

    • @coreyms
      @coreyms  6 лет назад +1

      Thanks! I appreciate the kind words

  • @user-zq4ix8ps6g
    @user-zq4ix8ps6g 6 лет назад +1

    best python tutorial I ever watched

  • @al-farabinagashbayev5403
    @al-farabinagashbayev5403 6 лет назад +92

    if you have new csv file with blank lines this is for you
    # Python 2
    with open('new_names.csv', 'wb') as outfile:
    writer = csv.writer(outfile)
    # Python 3
    with open('new_names.csv', 'w', newline='') as outfile:
    writer = csv.writer(outfile)

    • @MilanRihaMilan
      @MilanRihaMilan 6 лет назад

      I had the same problem :P ...newline='' works just fine. Thank you.
      Great lectures, I really like the way you teach it. ;) not too fast, not too slow, good examples, easy to understand, really brilliant teaching skills ;)

    • @jamesgu9223
      @jamesgu9223 5 лет назад

      thank you, your way works fine.

    • @kitayuan9842
      @kitayuan9842 5 лет назад

      cheers brother

    • @sergiomedina5520
      @sergiomedina5520 4 года назад

      I was spending 15 minutes to resolve it until you saved me with your comment. Thanks a lot!

    • @granberyacademia
      @granberyacademia 4 года назад +1

      I wish I had read the comments... I couldn't understand why my code was f-ing me up

  • @madyrebecca
    @madyrebecca 6 лет назад +6

    you're videos are magic !! i love watching them , thank you so much !!!!!!!

    • @MyTube4Utoo
      @MyTube4Utoo 6 лет назад +1

      +madyrebecca Corey's videos are 'magic.' I've started on Python books that just bored me. I've used Udemy, CodeCademy, other RUclips videos, as well as countless other tutorials, but *nothing* has helped me nearly as much as Cory's tutorials. So yeah, "magic" applies here. I bet he's a warlock! lol

  • @miquelr2353
    @miquelr2353 24 дня назад

    Thanks for being so clear. Learning by yourself can be like decyphering hierogrypics

  • @emilyncozzens8309
    @emilyncozzens8309 Год назад

    This is the best tutorial the I never found! Thanks so much!

  • @mediumshrimp9696
    @mediumshrimp9696 3 года назад +2

    I have never seen a negative comment on your videos. You are helping me get through this coding boot camp like you'll never believe. Thanks, Mr. Corey.

    • @okayest_pianist
      @okayest_pianist Месяц назад

      how'd the bootcamp go bruh

    • @mediumshrimp9696
      @mediumshrimp9696 Месяц назад +1

      @okayest_pianist For me it was worth it. My class size was 17 or 19 and only two didn't get jobs right before/after graduation. Several of my classmates are going to college either while online/in person to get degrees. It is hard, our starting class size was 28. In my opinion, it's the way to go.

    • @okayest_pianist
      @okayest_pianist Месяц назад

      @@mediumshrimp9696 love to hear it brother. glhf

    • @atienograce2520
      @atienograce2520 Месяц назад +2

      I’ll be joining a data analytics bootcamp this September and this comment gave me hope.Yes,it won’t be all rosy but it will be worth it!Thank you!

  • @danmuniz1354
    @danmuniz1354 5 лет назад +1

    Thanks Corey. Very Helpful! Cheers!

  • @organizedchaos4559
    @organizedchaos4559 3 года назад

    Great video, very informative and helpful. You make it really easy to follow and understand

  • @husseinabukar7081
    @husseinabukar7081 3 года назад

    Absolutely, fantastic video and simple to understand it. Many thanks for your time and efforts

  • @ozodbekulashov2325
    @ozodbekulashov2325 4 года назад +2

    Hello Mr Corey ! Thanks for this incredible content!

  • @cyperdevelopment
    @cyperdevelopment 5 лет назад +1

    Your introduction voice is amazing!

  • @magicboy550
    @magicboy550 5 лет назад +1

    Thank you so much for the sucint tutorial. This precisely what I needed!

  • @Nkraka
    @Nkraka 6 лет назад +5

    First of all Corey, I will like to say that you're an amazing teaching. You've inspired lots of people especially someone like me that is pretty new to Python. Your videos are awesome. Just keep up the good work we really do appreciate you. Thank you! With that been said, Corey, I have been searching and googling almost everywhere like stack overflow and many other python communities to see if I can at least find maybe an examples on how to work on or handle large files in python -- files ranging from 100 GB and up. Unfortunately, I haven't seen anything that actually explains the concept clear enough. So, I was wondering maybe if possible if you will consider making a video just for that purpose. Basically, the program should be able to read, chunk and write the file(s) or segments into a folder based on numbers of lines or size specified by the user. Please I will really appreciate it if you can help. Thanks in advance!

    • @coreyms
      @coreyms  6 лет назад +1

      I don't have a video specifically on large files, but if you watch my video on File Objects, I do go over reading a file in chunks. If you search my channel or Python playlist for "File Objects" then you should find that.

  • @TheFrazerboy
    @TheFrazerboy 2 года назад

    excellent stuff definitly helping with my masters project

  • @navidpanchi
    @navidpanchi 6 лет назад

    Thank you so much for such a wonder full series!

  • @kasianowak1389
    @kasianowak1389 6 лет назад +2

    Hi Corey, your videos are just great. Would it be possible to show us how to iterate the nested dictionaries

  • @drewconroy5938
    @drewconroy5938 6 лет назад +1

    Great stuff as always - Thank you:)

  • @x0gucx
    @x0gucx 7 месяцев назад

    Thank you SOOOO Much, even after 6 years this tutorial is so useful. my college professor just told use to do this with an article attachment and it's an intro class so I couldn't understand a single thing on there. THANK YOU FOR THE VIDEO TUTORIAL❣

  • @diggable
    @diggable 6 лет назад

    awesome, my first CSV read\write tutorial! thank you sir!
    -------------------------

  • @amanuelsiyum4537
    @amanuelsiyum4537 5 лет назад +1

    Very helpful videos, keep up the good work!!!!!

  • @mahodaadikari4116
    @mahodaadikari4116 Год назад +1

    Thanks for the wonderful explanation ❤💫💪🏼

  • @muhammadnaveedkhan6739
    @muhammadnaveedkhan6739 2 года назад

    Excellent work Sir, Your teaching style is awesome.

  • @FisVii77
    @FisVii77 6 лет назад

    man you are a very good python presenter, keep up the great work and vids.

  • @goldenogregames
    @goldenogregames 3 года назад +8

    For anyone getting spaces between lines using writerow method ( using python 3 ), you need to add newline=' ' inside open like this, open('file.csv', 'w',newline=' ') as ... : (the second with open iteration)

  • @datag1199
    @datag1199 Год назад

    Great tutorial - thank you! Subscribed

  • @kgsanjuana
    @kgsanjuana 6 лет назад +8

    You are amazing! Thank you so much!!!! I have a better understanding of csv and can apply it to my school assignment.

    • @assasin101011
      @assasin101011 6 лет назад

      don't go to school just code in your house and you can get a job in no time I promise you that. I'm 100% sure i swear this to our generation and mankind

  • @jonusbrothers2067
    @jonusbrothers2067 6 лет назад +1

    great video sir..! :) thanks for uploading it....and can you make more videos on python most used libraries like you made on csv?

  • @kimberlybacchia1413
    @kimberlybacchia1413 4 года назад +1

    SO helpful, thank you!!

  • @akram42
    @akram42 6 лет назад +7

    to be honest you're the best instructor I've even seen on youtube, can you please make tutorial on simple website that when the user submits a form python code will run to get for example google result . I've been trying to do this for a while now still no luck.
    I just what to figure out how to run python code from html whenever the visitor searches or interacts with the website.

    • @coreyms
      @coreyms  6 лет назад +6

      I would really like to put together some web framework tutorials. I have some scripts in the making, but it will likely take a while longer to get everything recorded and edited. Hopefully in the near future.

  • @egonz7194
    @egonz7194 3 года назад

    just what i needed for my problom set thanks bro .

  • @impact_k
    @impact_k 9 месяцев назад

    This is fantastic, no lies. Thank you very much

  • @aninehartog3950
    @aninehartog3950 3 года назад +1

    Your videos are fantastic, thank you for all your time and effort! I just have one question. How do you read through the csv file and had earch row to a prosgresSQL table?

  • @ricardolazaro1766
    @ricardolazaro1766 3 года назад +1

    Awesome video, I very easy to understand! Thank you for the awesome video!

  • @letshev
    @letshev 3 года назад

    Corey, you are the best. Thank you.

  • @bluesdog88
    @bluesdog88 3 года назад

    Great tutorial Cory, thanks so much, spent most of my weekend looking for ways to read a text file, and your tutorial showed me in 5 minutes lol. For an assignment I have to print first last name from a text file(done!) but the file has Y,Y or Y, , data at the end to show if the people are going to a conference, they get a pack, going bot st and Sunday the get an extra pack( packs read from another file) do i just do some else if statements to get that data or is there a better way now I have list data(hope that makes sense!)

  • @Ninja-iq2xt
    @Ninja-iq2xt 6 лет назад +14

    I think and i hope you are on a path on start tutorials on pandas, and i guarantee that it could be the blockbuster series, people will love your quality videos and teachings. Their are no quality videos on pandas on youtube.

  • @PrashantKumar-ks2rd
    @PrashantKumar-ks2rd 5 лет назад +1

    really helpful video thank you!!

  • @martin-xq7te
    @martin-xq7te 4 года назад

    Great video training. just what I was looking. What about a video on collecting frequencies from a csv file

  • @ruizhenmai1194
    @ruizhenmai1194 6 лет назад

    concise and clear! thx so much

  • @timo_b3
    @timo_b3 2 года назад

    Awesome, thank you corey!

  • @converti76
    @converti76 4 года назад +1

    Super useful! Thanks dude!

  • @MexicanPickleTamarindo
    @MexicanPickleTamarindo Год назад

    Better than my professor! Thank you.

  • @simonnguyen692
    @simonnguyen692 Год назад

    sick tutorial man thank you

  • @daisycarranza4034
    @daisycarranza4034 Год назад

    Thank you so much I needed this

  • @shivrankrishen
    @shivrankrishen 2 года назад

    Short and down to the point.You Sir you're a God Sent.

  • @monroekass
    @monroekass Год назад +1

    Thanks, Corey for the tutorial. How do you write data starting from a specific column or/and row in CSV?

  • @exili
    @exili 6 лет назад

    Beginner here, your tutorials have been a my go to! Thanks for the content. I am currently trying to export Webscraped data (using selenium, and creating a tuple using 'in zip' for the extracted data). But I am having trouble outputting to a CSV, anyway you can help with this? What is the best way to accomplish this?

  • @ritheshbcanvas4963
    @ritheshbcanvas4963 5 лет назад +1

    Super .. helpfull.. Please do this kind of real word problems..

  • @sphiwemodise5713
    @sphiwemodise5713 4 года назад +1

    Thank you. This is helpful

  • @praveenmishra5822
    @praveenmishra5822 2 года назад

    Nice explanation, Thanks!

  • @tobbysorama
    @tobbysorama 6 лет назад +1

    You are an awesome tutor!

  • @karmendurbin
    @karmendurbin 2 года назад

    Amazing explanation.

  • @sujeetkumarranjan9947
    @sujeetkumarranjan9947 4 года назад +1

    Thank you for this video, it was amazing.
    Can you please let me know how to write one element of row at one time in CSV file and not whole row at one go?

  • @takibahmed8859
    @takibahmed8859 2 года назад

    Thank you for this lecture

  • @mjprescotti
    @mjprescotti 3 года назад

    This video was worth the viewing I wish I had found this days ago:)

  • @chaitanyayeturi1312
    @chaitanyayeturi1312 2 года назад +2

    First of all, your teaching style is good, and anyone grabs ur teachings easily, and thanks for teaching us. you are the best tutor I have ever come up with in python language. can u suggest any python language book, which is best to go through other than your channel? Moreover, I am a beginner to python language I thoroughly enjoyed ur videos, if u can suggest me best python book to ur sensibility and I guess it suits me and it helps me to learn further (I like physical like books). In case if u written any books related to this language can u suggest one (or) your favorite book, please......................................

  • @muralidhar40
    @muralidhar40 Год назад

    I can not quite put the finger around it, but your teaching is magical!

  • @omkarkhare4542
    @omkarkhare4542 5 лет назад +1

    Very well explained

  • @jaimevalencia3734
    @jaimevalencia3734 6 лет назад +1

    wow thank you for this, great explanation! Question, say i wanted to make an interactive search for a large csv database, that asks user input i.e "enter user name" and then it prints out e-mail or any other value in said row? :)

  • @asifahussain899
    @asifahussain899 Год назад

    U are great Corey Sir ... waiting for Ur 1 million subscribers ❤️

  • @prateekmohanty8315
    @prateekmohanty8315 2 года назад

    Amazing video man , thank you so much

  • @yingbowang3068
    @yingbowang3068 3 года назад

    exactly what i want,Thanks !!

  • @ivanyakushchenko6915
    @ivanyakushchenko6915 6 лет назад

    Great video! Thanks a lot for your lessons! I'm just curious, whether it's possible to apply DictReader method to csv file, which would have header not in the first line, but below? I have firt 6 lines as some metadata, and then 7th line - header, and remaining - data according to headers structure...