Python Tutorial: CSV Module – How to Read, Parse, and Write CSV Files
Video Title: Python Tutorial: CSV Module – How to Read, Parse, and Write CSV Files
Hey there. How’s it going everybody in this video? We’re going to be looking at how to read parse and write CSV files now if you don’t know what CSV files are it stands for comma separated values basically CSV files allow us to put into a plain text file
Some data and use some type of delimiter usually a comma to separate the different fields now I have a sample CSV file here that we can work with and if we look at this then we can see how these are
Usually structured so this can kind of look like a mess, but it’s not really meant to be read directly This is just how the data is stored And then we can use our programs to parse out the information that we want, but we can see that the top
Line here has our fields now our fields in this file are first name Last name and email so that tells us the information that we should expect to see on every line so if I go to the next line here
Then we can see that John is the first name and then a comma though is the last name and then a comma and then This long email here is the email so that’s why these are called comma separated values and what separates two values is called a delimiter
So the comma is a common delimiter But you can use just about anything so sometimes you’ll see files with tab delimited values or dashes or things like that But they’re all called CSV files. So now let’s see what it’s like to read parts and write to CSV files
So I have a file here called Parse CSV pie and within this file We’re just going to import CSV now you may have looked at that Data and wondered Why we’re just not using it the strings split method on each line of the file to parse out the data and you could do
That but the CSV module just makes parsing these files so much easier so for example if someone puts a comma or something in their name for some reason then we wouldn’t want to split on that and
Also the CSV module will handle new lines and all those things so it just makes it a lot easier to parse out all the Information that we want without writing something complicated from scratch okay, so to read the CSV file We’re just going to open this file. Just like any other file
So we’ll use a context manager here And we’ll say with open and the name of that file that I was just looking at is now CSV, and it’s in the same directory as the file that I’m currently writing and we want to read this file
So we’ll put an r there as the second value and now what we want to call this so we’ll say CSV file So to read this file We can say CSV reader and that can be any variable name that you like, but that’s what I like
And then we can say CSV and then use this reader method and then pass that CSV file in to that reader method now in the background that reader method is using something called a dialect that has some preset parameters
For what it expects the format of our CSV file to be so by default it’s expecting values to be Separated by a comma and a few other things that we’ll look at in Just a bit, but since our CSV file is pretty simple We don’t need to pass any additional arguments right now
So the CSV reader variable that we just created is going to be something that we need to iterate over So for example if we just print this out as is so print CSV reader and run that then we can see that right now. It’s just an object in memory, so instead
We need to loop over all these lines in the reader and see what we get so we can say for line in CSV reader and then print out each line And we’ll run that ok so that looks better so each line that
We’re printing out is a list of all the values so the first value in the list is the name as the first name The second value in the list is the last name and the email is the third value if I scroll all the way up to
The top you can see that our first line is the field names so it tells us that the you know first value this first name last name is the Second value and Third value is email so for example if we’re going by the
Index like this would be index 0 and then 1 and then the email would be index 2 if we only wanted to print out All of the indexes then on this line here, we could say let’s print out index 2 of each line
And if we run that and we can see that now we get all of the emails printed out now if you don’t want this First line of the field names and only want the values then we can just skip that first line so if anyone has seen my video on generators
And we can actually step over value an iterable by calling next and running next we’ll Return the next value if we want to capture that in a variable But if we just want to step over the value then we can come up here before our list. We can just say next
CSV reader and that will loop over that first line And then when we iterate through this it should start at the second value Which is the first person in the list so now if we rerun this and scroll up to the top?
Now we can see that that John doe is now the first value, okay? So now let’s see how we can write to a CSV file now We can do this with any list values, but since we’re already have a list of values here from our original CSV file
Let’s go ahead and just use those so let’s say that we wanted to save these same values into a new CSV file But use dashes instead of commas for the delimiter now
Dash is probably isn’t a great delimiter, but I just want to show you something that happens when we do this now first We’re going to want to write the field name headers into the new file So let’s take out this next statement where we’re skipping over those so
Now I’ll come down here and now actually above our loop we’re going to want to open a new file for writing and So we’ll say with open and we’ll call this file new underscore names CSV We want to open this for writing
So the second argument is aw, then we’ll say as and we’ll just call this a variable new file and to write to this file We’re going to use a CSV writer so we can say CSV writer and that can be any variable name
But that makes sense to me and we’ll do CSV and then use this writer Method and we’re going to pass in new file to that writer method now if we left it like this then it would just write the same comma
Separated file that we currently have now but if we want to use dashes as our delimiter Then we need to pass that in as an argument So it’s going to be the second argument to that writer method and we can say delimiter equals And we’ll just go use a dash now
We want to write each line of our original CSV file into this new file, so let’s indent our for loop over here So that now we’re within the context manager of this new file and for each Line in this CSV reader. Which is our original file
We want to write that to a new file so we can do that by saying CSV writer dot write Row and The row that we want to write is that line from the original reader?
So real quick before I run this we are opening the original file to be read and then we’re creating this CSV reader variable and we are using the CSV reader method to read that original CSV file and then we’re opening a new file for writing called new Names CSV and
Then we’re creating a CSV writer variable and we’re using this writer method of the CSV module to Open up a writer using that new file with a delimiter of a dash and then for each line in this original
CSV data we are writing out to the new file each line of the original file So now if we run this then we don’t have any output here at the bottom But it should have created this new file called new named CSV and I’ll go ahead and open that up now
We can see in this new file that. It’s using dashes instead of commas for the delimiter now This makes it pretty hard to read, but I wanted to show you what it did with two of our values here
So in our first value the email actually contained a dash so we can see here that our CSV writer knew to put quotes around the email Since it can’t contain that delimiter And that’s so when the CSV is read back in that it would know that the email is
One whole value and that it shouldn’t be split on the dash within the email itself and likewise here We can see that our second person here has a hyphenated last name of Smith Robinson
So again the CSV writer knew to put quotes around the last name so that it can tell the difference between the delimiters and the values that just happen to contain dashes So now that we’ve seen how that works let’s actually change this delimiter for the new file to something
That’s a bit more common, so aside from Commas tabs are very common Des limiters So let’s use tab instead and in python the tab can be represented with this backslash t and if we rerun that and then open up the new names file again
Then we can see that now all the values are separated by tabs instead. That’s a lot more easier That’s a lot more easy to read now Just like we passed the delimiter into our writer if we wanted to read in that tab delimited file
Then you could pass the delimiter argument into the reader as well, and Real quick let me show you what that would look like if we tried to read a CSV file with the wrong delimiter So let me copy part of this here where we’re reading in this file
And now I’m just going to comment out everything else for now now instead of reading the original file names CSV We’re instead going to read the new tab delimited file that we just created which is new underscore names CSV now Let’s pretend that we forgot to
Specify the Tab delimiter and just try to read this as is so let’s print out the lines that we get from this reader So we’ll say four line in CSV reader and we will print out each line So we can see that each line only has one value
And it didn’t split on the values on the tab because it was expecting commas So instead you have to explicitly pass in that we want the delimiter to be a tab so I’ll pass that into the reader method here and say delimiter equals a
Backslash t for tab and then rerun that and now you can see that we get the correct parsing okay? so now I’m going to delete these lines here and Uncomment out what we had before
Okay, so the way that we’ve been working with CSV files using the reader and writer is probably the more common way to work with CSV data since they’re the first things that come up in the python documentation But my preferred method is working with CSV data
Using the dictionary reader and the dictionary writer So let’s take a look at those and I’ll explain why I prefer them over the regular reader and writer okay, so first Let’s take a look at the dictionary reader
So to use this we’re just going to replace the regular reader method here with a dict Reader and now let’s print out the lines that we give with this so I’ll say four line in CSV reader and we’ll just print out each line
Okay, so at first glance this may look a little more complicated each of the values is now an ordered dictionary and if we scroll Up here to the top then we can see that that first line no longer contains the field names It starts off immediately with the first person
So the reason is that the field names are now the keys of each of these values here now the reason I like this is because it makes it a lot easier to parse out the information that we want so for example remember when we
Use the regular reader if we wanted to print out the email Address then we printed out the second index of our line well for anyone reading your code It isn’t obvious what that second index is so they’d have to go into the CSV file to find that information out
But now that we have those Fields as our dictionary keys then we can get the email here by saying I just want the Email of that line So we just access that key so now if we rerun that we can see that now we have all of the email information
Okay, and now let’s look at how to use the dictionary writer So I’m going to remove this loop and then uncomment out the rest of this information here Now with the dictionary reader. We really didn’t need to change anything, but with the dictionary writer
We actually have to provide the field names of our file, so one line above our writer here I’m just going to create a list of the field names And now instead of using this writer method. We’re instead going to use dict writer
Now one thing that we need to change there here is that after the file that we’re going to be writing to we need to? Pass in those field names, so I’ll say field names is equal to field names
Okay, and now we’re ready to write the data so with the dictionary writer you have the option of whether or not you want to Write out those headers. Which are the field names in the first row so if we want those headers, which most of the time I do then we can say
CSV writer Dot right header So that’s going to write out those field names as the first line and once the header is written out We can loop through the lines of the original file
Just like we did before and say CSV writer dot right row and then pass in that line so all of that stays the same so if we run this and then look over here at our new names CSV file then we can see that that still worked and
Like I said before the reason I like working with the dictionary reader and writer is because it’s more obvious what you’re doing So let’s say for example that in our new CSV file
We actually only wanted the first and last names and wanted to leave off the email well with the regular reader and writer We’d be modifying the indexes of those list and like I mentioned before it’s not obvious by looking at an index
What value it’s supposed to hold but with our dictionary writer? We can just remove the email from the field names up here and Before we write each line within our loop here
We can just remove the email key and value and one way to do that is to just delete it so we can say delete the email of That line so now when it writes that row it’s only going to be writing the first name and the last name and the email
No longer exists So now if we save that and run it and then I open up the new names dot CSV file here then you can see That now we just have a tab delimited file of first names and last names and that email is no longer there
Now there are several ways that we could have written this row. We could have deleted the email key from Line Just like we did here or we could have created a new dictionary? With only the first name and last name keys and passed that into the right Road method
So whichever way works for you in this case. I think it was easier Just to remove the email key okay, so I think that is going to do it for this video I hope that now you have a pretty good idea for how you can read parse and write CSV files
But if anyone does have any questions about what we covered in this video then feel free to ask in the comment section below and I’ll do my best to answer those and if you enjoy these tutorials and
Would like to support them and there are several ways you can do that the easiest ways to simply like the video and give it a thumbs up and also It’s a huge help to share these videos with anyone who you think would find them useful and if you have the means you can
Contribute through Patreon And there’s a link to that page in the description section below be sure to subscribe for future videos and thank you all for watching you
-
Sale!
Wireless WIFI Repeater Extender Amplifier Booster 300Mbps
$29.99$14.99 Add to cartWireless WIFI Repeater Extender Amplifier Booster 300Mbps
Categories: Electronics, Wi-Fi Router, Wireless Wi-Fi Extender Tags: 300Mbps, 802.11N, Amplifier, Booster, Extender, mobile wi-fi booster, Remote, WIFI, Wireless, Wireless WIFI, Wireless WIFI Repeater, Wireless WIFI Repeater Extender, Wireless WIFI Repeater Extender Amplifier, Wireless WIFI Repeater Extender Amplifier Booster, Wireless WIFI Repeater Extender Amplifier Booster 300Mbps$29.99$14.99 -
Sale!
Full RGB Light Design Gaming Headset Headphones with Mic
$24.99$14.99 Add to cartFull RGB Light Design Gaming Headset Headphones with Mic
Categories: Electronics, Gaming, Gaming Headsets Tags: Design, Full, Full RGB Light Design Gaming Headset, Full RGB Light Design Gaming Headset Headphones, Full RGB Light Design Gaming Headset Headphones with Mic, Gamer, Gaming, Gaming Headset Headphones, gaming headset wireless, Headphone, Headphones, Headset, Light, Mic, Package, RGB$24.99$14.99 -
Sale!
Wireless BlueTooth Multi-Device Keyboard Mouse Combo
$39.99$19.99 Add to cartWireless BlueTooth Multi-Device Keyboard Mouse Combo
Categories: Electronics, Gaming, Gaming Keyboards, Keyboard Mouse Combos Tags: Combo, Keyboard, keyboard mouse combos, Mouse, MultiDevice, Set, WireKeyboard Mouse Combo, Wireless, Wireless BlueTooth Keyboard Mouse Combo, Wireless BlueTooth Keyboard Mouse Combos, Wireless BlueTooth Multi-Device Keyboard Mouse Combo, Wireless BlueTooth Multi-Device Keyboard Mouse Combos$39.99$19.99 -
Sale!
High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
$199.99$139.99 Add to cartHigh Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
Categories: Gaming, Gaming Chairs Tags: Adjustable, Chair, computer chairs, Desk, Executive, Gaming, Girl, Headrest, High, High Back Leather Executive Adjustable Swivel Gaming Chair, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar, High Back Leather Executive Adjustable Swivel Gaming Chairs, Leather, Lumbar, Office, Racing, Swivel$199.99$139.99 -
Sale!
Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
$29.99$19.99 Select optionsProfessional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
SKU: N/A Categories: Electronics, Gaming, Gaming Headsets Tags: Cancelling, Gaming, Gaming Headphones with Noise Cancelling Microphone, gaming headset, Headphones, Headset, LED, Light, Mic, Microphone, Noise, Professional, Professional LED Light Wired Gaming Headphones, Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone, Wired, Wired Gaming Headphones, Wired Gaming Headphones with Noise Cancelling Microphone$29.99$19.99 -
Sale!
Gaming Desk with LED Lights USB Power Outlets and Charging Ports
$349.99$249.99 Select optionsGaming Desk with LED Lights USB Power Outlets and Charging Ports
SKU: N/A Categories: Computer Desk, Gaming, Gaming Desk Tags: and Charging Ports, Charging, Desk, Desks, Gaming, gaming desk with led lights, Gaming Desks with LED Lights, Home, LED, Lights, Monitor, Office, Outlets, Port, Power, Room, Stand, USB, USB Power Outlets, White, Workstation$349.99$249.99 -
Sale!
Wired Mixed Backlit Anti-Ghosting Gaming Keyboard
$99.99$79.99 Add to cartWired Mixed Backlit Anti-Ghosting Gaming Keyboard
Categories: Electronics, Gaming, Gaming Keyboards Tags: Antighosting, Backlit, Blue, brown, Gaming, Gaming Keyboard, gaming keyboards, gaming keyboards and mouse, Keyboard, Laptop, Switch, Wired, Wired Mixed Backlit Anti-Ghosting Gaming Keyboard, Wired Mixed Backlit Anti-Ghosting Gaming Keyboards, Wired Mixed Backlit Gaming Keyboard$99.99$79.99 -
Sale!
Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
$119.99$59.99 Add to cartWireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
Categories: Electronics, Gaming, Gaming Headsets Tags: 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, ANC, Audio, Bluetooth, Cancellation, Ear, Earphone, gaming headset, Headphones, Headset, Hi-Res Over the Ear Headphones Headset, HiRes, Noise, Wireless, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Headphones, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headsets$119.99$59.99 -
Sale!
Wired Sports Gaming Headset Earbuds with Microphone
$19.99$9.99 Select optionsWired Sports Gaming Headset Earbuds with Microphone
SKU: N/A Categories: Gaming, Gaming Headsets Tags: Accessories, Earbud, Earphone, Earphones, Gaming, gaming headset with microphone, Headphones, Headset, IOS, Microphone, Sports, Wired, Wired Sports Gaming Headset Earbuds, Wired Sports Gaming Headset Earbuds with Microphone, Wired Sports Headset Earbuds$19.99$9.99 -
Sale!
150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
$49.99$29.99 Add to cart150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
Categories: Charging Stations, Electronics Tags: 150W, 150W Charging Station, 150W Universal Multi USB Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Stations, 150W Universal Multi USB MAX Charging Station, 16 Port MAX Charging Station, 3.5A, Charger, Charging, Fast, laptop charging stations, Max, Multi, Port, Stand, Station, Universal, USB$49.99$29.99
Hi, I have a CSV file that contains entries such as "78 MPH" {TAB} "107 Yards" (it's from a golf monitor). I can read the file in but would need to convert each value from "78 MPH" to 78 as an integer (I want to calculate the average, max etc). How can I iterate each line to strip the text out and convert it to an integer? There's a beer riding on this Corey!
Thanks!
my csv file only returns '