HomeОбразованиеRelated VideosMore From: Corey Schafer

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

2772 ratings | 184418 views
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: https://github.com/CoreyMSchafer/code_snippets/tree/master/Python-CSV If you enjoy these videos and would like to support my channel, I would greatly appreciate any assistance through my Patreon account: https://www.patreon.com/coreyms Or a one-time contribution through PayPal: https://goo.gl/649HFY If you would like to see additional ways in which you can support the channel, you can check out my support page: http://coreyms.com/support/ Equipment I use and books I recommend: https://www.amazon.com/shop/coreyschafer You can find me on: My website - http://coreyms.com/ Facebook - https://www.facebook.com/CoreyMSchafer Twitter - https://twitter.com/CoreyMSchafer Google Plus - https://plus.google.com/+CoreySchafer44/posts Instagram - https://www.instagram.com/coreymschafer/ #Python
Html code for embedding videos on your blog
Text Comments (242)
LALIT BHAVSAR (2 days ago)
How about column how separate the also like row.. please replay
Hamza Mahmood (5 days ago)
i m giving list to csv.writerow butit is taking it as a single value and not removing brackets
Thiyagu Mani (9 days ago)
csv module not found in pip
yadi liu (12 days ago)
Thanks for the helpful video! I'm new to Python... here in the video I don't understand why the output is not a dictionary format with DictWriter. Can anyone explain it to me please? Thanks
Pedro Rodrigues (13 days ago)
Does this works for a document .txt?
Corey Schafer (13 days ago)
Hmm, that's a good question. I would assume it would because I don't think Python pays any attention to file extensions. It's definitely worth testing though.
estephanie gomez (14 days ago)
Hello Corey, Thanks for the tutorials, do you have a tutorial using csv module with tkinter? I would like to edit csv files using a GUI interface in python. Regards, Estephanie
Naveed Rajput (18 days ago)
It was fantastic , i just want to learn about online data downloading , i am able to download history data from net but when i go to write in csv file it omits some record and place dot dot dot (omit data) and in last it write some lines i am confuse how to handle that data pleas help
John Liang (18 days ago)
This video is the best, I watched many videos on YouTube, only this one really help
Tan Vorn (23 days ago)
Man how to deal with Non-Ascii characters?
Kyriakos Ketikidis (26 days ago)
How can I save my csv file in a folder and make my python script open it from there? For me it works only if I have saved my csv in my Desktop
bull shit
Dan Muniz (1 month ago)
Thanks Corey. Very Helpful! Cheers!
Touay (1 month ago)
Good vid. I hope you don't mind, and I know I am being a touch pedantic here, but for your beginners who are not used to american pronunciation, it would be more clear if pronounced "writer" with a t, rather than it sounding like "rider". I am English and it gave me a moments confusion, only rectified by reading the text, so I have to think the international people who may only be listening, could be confused.
Tahajjat Begum (1 month ago)
can u tell me how we can do customer segmentation using jypether
Ayushi Dutt (1 month ago)
where is the csv file saved ?
Kirup Vela (1 month ago)
You covered very well through explaining simple way. Thank you very much
_SealedKiller_ (2 months ago)
You can parse this without any module too
Erkam TOKGÖZ (2 months ago)
How do you put "#" on more than one line at the same time? (09:38) Thank you for your answer.
Hoàng Lực (2 months ago)
May I ask you a question? When I use csv.writerow I alway have a blank row after each row what have data, can you help me to fix it?
Prashant kumar (2 months ago)
really helpful video thank you!!
Gabe Braden (2 months ago)
Hey Corey! I was wondering how you get your csv file to "talk" to your python code. Whenever I try to run my code in my terminal, it gives me an error saying that "Traceback (most recent call last): File "csv_to_python.py", line 5, in <module> with open("app.csv", "r") as f: FileNotFoundError: [Errno 2] No such file or directory: 'app.csv'" Thank you so much!
Corey Schafer (2 months ago)
Are you sure you have your CSV in the same directory as the Python script you're running? If it's not in the same folder then it won't know how to find it. If you want to have the CSV file in a different location then you will need to pass in the full path to the CSV into the open function.
Andri Hidayat (2 months ago)
I already searching for this tutorial for some years, but finally I found it here. I don't know if I didn't found your video maybe my life is more difficult. Hope you continue in making very good quality video like this. I must subscribe your channel . Thank you so much.
tamil stalin (3 months ago)
Hi Corey, If possible , can explain how to use TextFSM module to parse collection of structure data to CSV format
Ajay R (3 months ago)
Hey Corey thanks for the awesome video . I have a question how do we get a user input and write that date to a particular row or coloum in csv file. I have a csv file which has name and team as feilds and some data in it. I would like to get user input and update my data row or coloum on it. Your help is appreciated. Pls feel free to contact me on my tweeter account as well @ajay_r_aj
azam khezri (3 months ago)
Hi I am writing nptconfig file I calculate dimensions fixed all not backbone set 300k as well as "p2npt_fixed.pdb" created now for writing a config file, I faced with this error atomsel: writepdb failed. atomsel: writepdb failed. while executing "$all writepdb ${basename}_fixed.pdb" (procedure "write_files" line 64) invoked from within "write_files" (procedure "::NAMDgui::config_only" line 3) invoked from within "::NAMDgui::config_only " (procedure "::NAMDgui::opendialog" line 96) invoked from within "::NAMDgui::opendialog save $::NAMDgui::namdconfig" invoked from within ".namdgui.go.write invoke" ("uplevel" body line 1) invoked from within "uplevel #0 [list $w invoke]" (procedure "tk::ButtonUp" line 24) invoked from within "tk::ButtonUp .namdgui.go.write" (command bound to event) can you help me?
Gene Yuss (3 months ago)
David Alexander (3 months ago)
I'm running 3.6 and threw an error, TypeError: "delimiter" must be a 1-character string. Solution: swapped and '/t' with ','
alhurayas m (3 months ago)
thank you good tutorial but writerow
Dev Slaven (4 months ago)
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!
sabarish amaravadi (4 months ago)
i am trying to read a csv file which looks like this ['261', '92'] ['276', '92'] ['276', '117'] ['261', '117'] and i have 2 strings in a row and 400+ lines. how can i convert this to array(x, 4 ) array or list or tuple and i also wanted to remove the repeated values. For ex. here i need 261,92,276,117 in one row.
Andrew Cekuta (4 months ago)
great video once again
Prabhakaran Subramaniyan (4 months ago)
its very nice video and with many information and tips:)
Rocky Xiao (4 months ago)
We could use a "csv_writer.writerows(csv_reader)" instead of a for loop of csv_writer.writerow(line).
Vish (4 months ago)
Nice video. How can I write headers and rows for nested dictionaries? E.g. full_dict = {first_dict:{'a':1, 'b':2}, second_dict:{'a':1, 'b':2}}
omkar khare (4 months ago)
Very well explained
Songtao (4 months ago)
Awesome video!
Hi! I wrote your code is sublime text 3 and when I tried to create new file 'new_names1.csv' with a tab as a delimiter I got empty lines after each name. As I see, there are no empty lines in your video. Why could this happen? Here's a screen: https://ibb.co/fotZby
Jonathan Pereira (4 months ago)
Great!!! \o/
Hi, Jonathan. Thank you very much for the link, it helped to solve the issue! Now the code works without creating empty lines
Jonathan Pereira (4 months ago)
I've found this as well, maybe could fix that issue: https://stackoverflow.com/a/3348664/9968674
Jonathan Pereira (4 months ago)
And send me details as OS, Python Version, etc...
Jonathan Pereira (4 months ago)
Hi Рустам, it's really intriguing me! Could you send me your whole code (zip)? [email protected] Then I'll try to figure it out here... (Just if you want...)
Rithesh B Canvas (5 months ago)
Super .. helpfull.. Please do this kind of real word problems..
Swapnil Sawant (5 months ago)
Bilal Khan (5 months ago)
Great! Your tutorials are awesome
André Neiva Liboreiro (5 months ago)
Hi Corey, I am runnig the same code as you did to get the info from Beatles albums on my example. The thing is that when I run this on repl.it environment, it works fine (see the link https://repl.it/@AndreNeiva/PleasedWarmheartedExperiment). However, when I run the same script on my GIT BASH Python I get unicoded values ('—' or '\xe2\x80\x94') for some keys (see the link here https://repl.it/@AndreNeiva/MeaslyInsistentArea ). PS: On my PC, I'm running a 3.6.4 Python - Anaconda w/ GIT BASH and I get the same trouble with the unicodes. I tried basically every decode and encode method combinations but none solve the problem.
Ivan Stepanoff (5 months ago)
And separate thanks for clear speaking!!! I understand all the words. Beautiful!
Ivan Stepanoff (5 months ago)
Can't be better.. And "ordered dictionaries" is my new discovery. )) I knew that method to constract dict, but didn't recognize, that the result was odered.
Valerie Wang (5 months ago)
OMG Corey you are soooooo helpfullllllllllll. I love you!
Jingyu Chang (5 months ago)
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!
Mahi R (5 months ago)
I am using Standard Python IDLE and for writing new csv it is showing as "FileNotFoundError: [Errno 2] No such file or directory: 'new_csv.csv'"
Mayur Kr. Garg (21 days ago)
Make sure you are passing the 'w' argument while writing.
Ross Crispin (6 months ago)
Hey Corey, thanks for your videos. I was wondering, how would I update a CSV file that has been created already? I'm currently working on a news website web scraping tool that parses the words in each headline. The idea is to create a content analysis program, so ideally I need the CSV file to update rather than overwrite each time it's executed. How would I go about this? Kind regards, Ross
Nick Larralde (6 months ago)
I know this is an older video so hopefully, someone is still looking at comments. When you did the "With Line in csv_reader:" how did it naturally know that the variable "line" should be a single line from the .csv file?
Nick Larralde (6 months ago)
Ah, thank you Corey. That makes sense. Am I correct to assume that for different iterables, the variable assignment could be different? In this case, a for <variable> assumes that the <variable> is separated by the line whereas a different object could behave differently?
Corey Schafer (6 months ago)
It's something I've looked up in the documentation. Python has objects that are "iterable", and when you loop over them they can return different objects. In this case, the csv_reader returns those lines.
sameer k (6 months ago)
Thank you very much Sir for creating such an easy to understand tutorial about reading writing csv file.
Joe Jurkash (6 months ago)
Do you have any tutorials on doing this without using import csv?
Corey Schafer (6 months ago)
I do not. Is there any specific reason you don't want to use the csv module? If you do this yourself then I supposed it would be a good learning opportunity, but if you are using it for a project then I would highly recommend using the tried-and-true built-in modules.
Thanks for share your knowledge with Spanish subtitles!
Corey Schafer (6 months ago)
You're welcome. It is actually people in the community who occasionally submit translation subtitles, so I have them to thank for that!
oykamal (6 months ago)
for TAB you can also use ",", in my compiler "\t" doesn't work.
when i attempt to write the code, it says, csv has no attribute 'reader'
Corey Schafer (6 months ago)
Did you create a variable called csv? It's possible to overwrite the built-in csv by doing that
jonus brothers (6 months ago)
great video sir..! :) thanks for uploading it....and can you make more videos on python most used libraries like you made on csv?
Вася Добра (6 months ago)
I fixed this eror. Sory!
Вася Добра (6 months ago)
I get an error on line 10 with csv_writer.writerow(line). Error is ValueError: I/O operation on closed file
Toons & Anime (7 months ago)
How do u create a CSV file
Toons & Anime (6 months ago)
Dimitri Moreira thanks
Dimitri Moreira (6 months ago)
with open("...", 'w', newline='') as myfile: ... The "w" mode will create a file if it does not exist. However if you're trying to write to a file AFTER reading from the same file, it will give you an error, since the file doesn't exist yet, you can't read from it.
veganath (7 months ago)
Thanks Corey I have a newbie question: Is there any way to easily know if a module function/method for a object is for example *next(csv_reader)* or *csv_**reader.next* given that csv_reader is an object, am I missing something here?
Xcentric (7 months ago)
is it possible to delete rows?
Derek Xue (7 months ago)
Hi could you tell me if it possible to get a csv file of all my soundcloud tracks ? Thank you.
maxi maroc (7 months ago)
Hi Guys, I can't find a module csv for python. Do you have same issue. I couldn't execute the code.
maxi maroc (7 months ago)
I copied from my original python lib to pycharm modules.
maxi maroc (7 months ago)
Corey Schafer it is not built in module in pycharm
Corey Schafer (7 months ago)
It's a built-in module, so you shouldn't need to install anything additional to get it. What error are you receiving?
Mike W (7 months ago)
I moved from Perl to Python hoping to find functionality like this. THANKS!
Ruizhen Mai (7 months ago)
concise and clear! thx so much
Mike fisvii (7 months ago)
man you are a very good python presenter, keep up the great work and vids.
Mindset Nuggets (7 months ago)
Hi Corey, You have really explained this CSV files manipulation in a very file simple way. Can you explain how I can write an output into a new csv file in an append mode with dialect='excel' and writing the header only once when the file is created. I am having trouble here because each time I rerun the program, the header is written in a new line again so I keep having to many lines with the header. Thanks a lot for your great tutorials.
Dimitri Moreira (6 months ago)
Your "myfile.writeheader()" must be inside a loop, hence why it keeps writing itself again and again. Just take it out.
Andreas Kaineder (8 months ago)
Hi Corey, great tutorials, really amazing stuff!! I have a question about deleting multiple columns: Do I have to write "del line['abc']" and "del line['xyz']" in two lines or is there a way to put it in one line of code? Thanks a lot!
Corey Schafer (8 months ago)
Hey there... you could possibly do that using the solution from this StackOverflow answer... https://stackoverflow.com/questions/8995611/removing-multiple-keys-from-a-dictionary-safely
Alexandre Mondaini (8 months ago)
Hey Corey, thanks for this lessons, you are great. Could you help me on a way to define the code in the end of the video in a way to have a function, so I could parse any csv file with it ?? Regards
Mariama Moura (8 months ago)
Nice video! I hope you can help me with my problem. I have to call a name (john, for example) and it needs to tell me his last name and email. How can I do this?
Arun Pandian (8 months ago)
greatsea (8 months ago)
how do i write Old Norse characters to a csv file?
Santosh kumar Singh (8 months ago)
Good clip You shared. Here step by step details shown to learn Csv data writing using Ax 2012. I hope you will like and subscribe https://youtu.be/RJtrQFgr__M
Kasia Nowak (8 months ago)
Hi Corey, your videos are just great. Would it be possible to show us how to iterate the nested dictionaries
Han (8 months ago)
pandas.read_csv is much easier. The result is a dataframe which is easy to index and manipulate.
李永胜 (8 months ago)
best python tutorial I ever watched
Chad Hanson (8 months ago)
I have a comma delimited Cvs file, but when I attempt to print out the third column nothing prints out. If I put in say [:2] I get three rows. Do I have the wrong type of cab? Saved from excel so maybe need that dialect. Here is code: import csv with open('chad_test_data.csv', 'r') as csv_file: csv_reader = csv.reader(csv_file) for line in csv_reader: print(line[2]) print('chuck')
Na Ma (8 months ago)
with csv file u can use pandas.read_csv below is the documentations https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html
Nakor Isilani (9 months ago)
When I tried this and wrote the line with csv_reader = csv.reader(csv_file) , the word reader after the dot didn't turn orange and there is an error on the line. What am I doing wrong?
tobbysorama (9 months ago)
You are an awesome tutor!
Sniipy MouàD (9 months ago)
Thanks Man :))
Matthew Williams (9 months ago)
i'm trying to write a date into a csv file (display date updated each time the python program is run. very puzzled could you help please
Bose s (9 months ago)
Hi Corey , are you having DATA SCIENCE tutorial.. I am planning to go into DATA-SCIENCE keeping python as my base..More if you have ML Tutorial that would be hugely appreciated... This Video is very helpful as always (clean and to the point).
Thomas Hultermans (9 months ago)
I notice that at 09:08, a lot of lines are not delimited by tabs but by spaces. Is the tab delimiter actually working or does the csv_reader make a mistake here?
Joan Hudson (9 months ago)
I really like your video. I want to ask about saving a list that you extracted in Python as a csv file. Your video is more of opening up a csv file that you already saved. But what if I extracted a list using Python and want to save it as a csv file? I'd appreciate your help.
Can't see anything
Chen Lenorld (9 months ago)
Hi Corey, thanks for your amazing video. I learnt a lot! I have a problem to open my CSV file. It shows "UnicodeDecodeError: 'utf-8' codec can't decode byte 0xae in position 265: invalid start byte". Could you please tell me how to solve this problem​? Do you have email address so I can shoot you an email when I have questions?
Bala Murugan (9 months ago)
Hello Corey. I followed the same code for a CSV file I have. It has headers id, firmname, cik. But it write all fields as a single column including the header like 'idfirmnamecik'. What am I missing here? Thank you so much for the videos!
Daniel Olsson (10 months ago)
Hi Thanks for your videos. For the last dictionary example. Is is possible to generate the fieldnames variable dynamically using the first row of data (assuming they contain clean header records)?
Puriwat Athichoktanaparn (10 months ago)
Can someone help me what am I wrong? __________________________________________ import csv with open('names.csv','r') as csv_file:      csv_reader = csv.reader(csv_file)      for line in csv_reader:            print(line) Traceback (most recent call last):   File "/Users/Puriwat/Desktop/Python Learning/python_csv.py", line 6, in <module>     for line in csv_reader: _csv.Error: line contains NULL byte [Finished in 0.1s with exit code 1] [shell_cmd: python -u "/Users/Puriwat/Desktop/Python Learning/python_csv.py"] [dir: /Users/Puriwat/Desktop/Python Learning] [path: /Library/Frameworks/Python.framework/Versions/3.6/bin:/Library/Frameworks/Python.framework/Versions /2.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin]
Rishabh Kalra (10 months ago)
hey? What if i want to update a specific row stored in a csv file? How can i do that?
confused (9 months ago)
Use a line counter and increment the counter in the "for" loop.  When you reach the line you want to edit, you can do so.
Navid Panchi (10 months ago)
Thank you so much for such a wonder full series!
Brodricks Franklyn (10 months ago)
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!
Corey Schafer (10 months ago)
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.
Rena Silva (10 months ago)
hi! Can you use the same arguments to read from a json file and write to a formatted csv file? How would I do this?
Corey Schafer (10 months ago)
Hey there. Yes, you could do this. Have you seen my video on working with JSON data? If you watch that then you will see how to turn that data into a dictionary and from there you could use the techniques in this video to insert those as rows.
All Day 8 (10 months ago)
Is there any way that you can access a certain email at an index? For example, in your video you printed out a list of every email, but if you wanted to access only one person’s email such that the console would only print out “[email protected]” can that be done?
Ray Bilton (10 months ago)
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.
Akhtar Shaikh (10 months ago)
Hi schafer.. Can you tell me how can i extract employees information from csv file using employee name.. If I give employee name in command line argument then I get all information in another file.. Like email, sal, cell number.. Thnks
TheFlyingduckman (10 months ago)
You sir, need a huge round of applause! Your video really helped me with an interview!
Corey Schafer (10 months ago)
That's great. Glad to hear it helped!
exili (10 months ago)
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?
lance dw (10 months ago)
I hate this shit, this will be the reason I fail at my exam tomorrow and probably cause me to fail my whole engineering curriculum... fuck python and programming, I want to be an engineer not a friggin programmer
Thinh Le (10 months ago)
hello Corey, Thanks for this guide. Can you tell me which program that you used in this tutorial. Thank for reading. Have a nice day.

Would you like to comment?

Join YouTube for a free account, or sign in if you are already a member.