# Python Tutorial: Real World Example - Parsing Names From a CSV to an HTML List

## Метаданные

- **Канал:** Corey Schafer
- **YouTube:** https://www.youtube.com/watch?v=bkpLhQd6YQM
- **Дата:** 26.07.2017
- **Длительность:** 18:48
- **Просмотры:** 191,672

## Описание

In this Python Programming Tutorial, we'll be learning how to parse a CSV file and output the data to an HTML unordered list. This is a real world problem I ran into with my website. The list was becoming too large for me to maintain manually, so writing a Python script to automate this process saved me a lot of time in the future. Let's get started.

The code from this video can be found at:
https://github.com/CoreyMSchafer/code_snippets/tree/master/Python-Patreon-CSV


✅ Support My Channel Through Patreon:
https://www.patreon.com/coreyms

✅ Become a Channel Member:
https://www.youtube.com/channel/UCCezIgC97PvUuR4_gbFUs5g/join

✅ One-Time Contribution Through PayPal:
https://goo.gl/649HFY

✅ Cryptocurrency Donations:
Bitcoin Wallet - 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
Ethereum Wallet - 0x151649418616068fB46C3598083817101d3bCD33
Litecoin Wallet - MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot

✅ Corey's Public Amazon Wishlist
http://a.co/inIyro1

✅ Equipment I Use and Books I Recommend:
https://www.amazon.com/shop/coreyschafer

▶️ You Can Find Me On:
My Website - http://coreyms.com/
My Second Channel - https://www.youtube.com/c/coreymschafer
Facebook - https://www.facebook.com/CoreyMSchafer
Twitter - https://twitter.com/CoreyMSchafer
Instagram - https://www.instagram.com/coreymschafer/

#Python

## Содержание

### [0:00](https://www.youtube.com/watch?v=bkpLhQd6YQM) <Untitled Chapter 1>

hey there how's it going everybody in this video we're going to be taking a look at a real world problem that I ran into and we'll walk through how to write a python script to solve this so I've done videos like this before and everyone seemed to find them useful now the difference between these videos and my normal videos is that I'm not going to go into as much uh stepbystep detail of every little step I'm just going to walk through how I came up with a solution and you can follow along so here's what I want to write a script to do so some of you may not know this but for anyone who contributes to this channel through patreon I list everyone on my website's uh contributors page as a small way of saying thanks well the problem that I'm running into and it's a great problem to have is that the contributors are getting up into the numbers where it's hard to keep track of who I've added to the site and who I haven't so I want to automate this process with python so that I don't ever Miss anyone and luckily patreon provides a downloadable CSV file of all the contributors which will make it easy to automate this process so if you don't know what CSV files are it stands for comma separated values and basically CSV files allow us to put data into a plain text file and use some type of delimiter which is usually a comma to separate the different fields so in this video we'll be learning how to use the CSV module to parse the CSV file count the contributors and then put their names into an HTML unordered list that I can drop into my website so let's go ahead and get started now first of all I don't want to expose anyone's information here so the CSV file I'm going to be using for this video uh takes out all of everyone's personal information and it just has fake names instead of the real names but other than the names being fake this is almost identical to the file that I downloaded from patreon so I'm going to open up this file it's called patron. CSV and when I first open up this file there are a couple of things that pop out to me when I first see this so first of all our first row is our headers and so we can see that it says that the information in this file is going to be first name last name email pledge lifetime status country and start now I really don't know what all those fields mean but basically I'm only concerned with the first name and last name so that's okay um also I noticed that there are a couple of lines here after the header that aren't actual data it's just a line explaining uh the people below this line are the ones who've said that they don't mind being listed on the website as a contributor and then it looks like the actual people start on line five here now on patreon you can also opt out rewards so there's likely a line in this CSV file that is a cut off for people who said that they only want to contribute but don't want to be listed on the website and actually if we look down here at line 35 uh we can see that cutoff point where it says that the people uh listed below this point do not want the reward and don't want to be listed on the website okay so now we have a basic idea of the data that we want to capture so now let's go ahead and start coding this so in the same directory I have a blank file here called parse CSV dopy and I'm going to open that up now first thing I'm

### [3:03](https://www.youtube.com/watch?v=bkpLhQd6YQM&t=183s) Import the Csv Module

going to do is import the CSV module and you may have looked at that CSV file and thought that hey that doesn't look difficult to parse so why not just use the split method on each line of the file to get that information and it's true that you could do that but the CSV module just makes parsing these files so much easier so for example if someone put a comma or something in their name for some reason then we wouldn't want to split on that and also the CSV uh module will handle new lines and everything like that and it just takes all the guesswork out of working with things like this so we're going to use the CSV module okay now I know that my end goal

### [3:42](https://www.youtube.com/watch?v=bkpLhQd6YQM&t=222s) Output an Html Unordered List

is to Output an HTML unordered list so I'm going to create an HTML output variable and set this to an empty string for now and we'll populate that as we go and I also know that I want to capture all the names of everyone that I want to add to that output so I'm going to create an empty list of the names okay so now let's open up our CSV file just like we would open up any other file so we're going to use a context manager here and we're going to say with open and this is called patron. CSV and we want to read this file so we're going to pass in an R there and we'll just call this a data file now I'm going to show

### [4:20](https://www.youtube.com/watch?v=bkpLhQd6YQM&t=260s) Ways To Parse the Csv File

you two different ways to parse the CSV file first I'll show you the most common and then I'll show you my preferred method so the first way we'll do this is

### [4:28](https://www.youtube.com/watch?v=bkpLhQd6YQM&t=268s) Csv Reader

with a CSV V reader um so I will say CSV data equals csvreader and now we're going to pass in that data file and actually let me make this text a little bit bigger here just so everyone can see as we're going along I think that's better so that reader method should have parsed the CSV file and put the data into our CSV data variable so let's print out what we have so far to make sure that it looks right so I'm going to come down a couple of lines here and just print out CSV data and run that okay so right now we just get this CSV reader object now you may have been expecting all of our CSV data now the data is there but this object is in iterable and behaves like a generator and what that means is that we have to Loop over it to get each line so you can either do that line by line or you can just convert it to a list and get all that data at once so if we converted this to a list and printed it out then we can see that it prints out a lot of information here in list form now it's not the easiest to read but it looks like our data so it's a good start so now let's actually print it out line by line so we can see this a little bit better so to do this we can say for line in CSV data and now we're just going to print out each line and run that and when we run that we can see that it is a lot easier to read so if we scroll up to the top and look at the first two lines we can see that the first line has the headers and we really don't need those other than to know which index each field is located so the first name is at index zero and the last one and it looks like the second line is the line uh telling us that these names uh we want to put on the website and then the third line is the first person uh with the name John do so we really don't need these first two lines here we just want to get the names of the people so if anyone has seen my video on generators we can actually step over values and an iterable by calling next so let's call next on our CSV data twice before printing out this Loop so we'll just say next CSV data then we will copy that and paste it in again now we don't need to capture the output from these and any variables we just want to throw them away so now if we run this and scroll back up to the top then we can see that now our first line is the first person of John Doe um okay so great now let's remove our print statement well actually before we do that um it's not obvious why we're running next twice on the CSV data here so it's important to comment non-obvious stuff like this uh while we're going along um not only for other people but for yourself also so you could come back to this code in a few weeks and have no idea why we ran these two lines here so let's just go ahead and make a comment that says uh we don't want headers or first line of bad data okay so within our Loop here We're looping over every person in the CSV file now remember that the first name is index zero and the last name is index one so let's go ahead and add each name to our list of names that we created at the top now to do this we'll say names. append now we want to append the a string of the first name space last name and to do this I'm just going to use an F string and then these uh braces for a placeholder and we'll say line and then index of zero for the first name then a space then another uh bracket for the placeholder and then index of one for the last name now like I've said if you've never seen a string with an F in the front like this is called an F string and they're new to python 3. 6 so if you're not using 3. 6 or later then this isn't going to work for you you'll have to use a regular uh string. format now I'm really liking these F strings so far and basically a it's a much simpler way of doing string formatting so if you'd like to see more about them then you can watch my video on strings where I go more in depth into all the different ways to format strings but basically all we're saying here is that we want a string uh with the value at index zero of the line which is the first name and then a space and then the value at index one of the line which is the last name okay and now that we've appended those let's print out all the names that were appended to that list uh since that's a global variable we can print that outside of our context manager all the way down here at the bottom so we're going to go down to the bottom and we'll say for name and names and then we will just print out the name okay so this is looking good it looks like we have the first names and the last names now if we scroll through our names here then we can see that one kind of sticks out and this is that no reward value so if you remember there are names in this list who it out and didn't want to be included so every name after this no reward value here uh shouldn't be added to our list well if we look back at our original CSV file here we can see that this no reward line has a comma after no reward so this should get parsed as a first name so let's add in a check for a first name of no reward and then we will break out of our loop as soon as we see that value So within our Loop here uh we will say if the index zero which should be the first name is equal to no reward then we are just going to break out of that Loop now before I run this uh we should note that the name before no reward over here in our file is Maggie Jefferson so when we rerun this uh this will hopefully be the last name in our list of names so I'll go ahead and rerun this and when we rerun that we can see that this fake name down here of Maggie Jefferson is the last name in the list so that works okay so now that we've tested to make sure that our names are right we can go ahead and just remove that list where we're printing or that Loop where we're printing out all the names now we're pretty close to being done here so the hard part is over now we just need to get these names into an HTML unordered list so that I can drop them into the site so first on the site I'd like to list how many supporters there are so we'll first add that to our HTML output with paragraph tags and to count how many there are uh we can just use the length of our names list so I'm going to say HTML output uh plus equals because we want to append to this then I'm just going to use another format string here um to put in these okay so like I said I used another F string here and we're using uh these HTML paragraph tags here that we're adding in now the only python data that I'm adding in is um the length of the names of that names list so when this gets printed out it should substitute the actual number there so just to make sure let's go ahead and print out this HTML output so print HTML output and I'll run that okay so apparently there are 30 people in that nam's list so now let's create our HTML unordered list with each name so we'll add an unordered list to our HTML output and I'll do this above where we're printing that output so I'll say HTML output plus equals and then an unordered list in Python now I'm going to put a new line there first an unordered list is uh this UL tag now that new line that I added in will just make it a lot easier to read when we actually print this out so now let's Loop through all of our names and add each one to an HTML list item um so if you aren't familiar with HTML then don't worry about it too much uh it's more about the process of just automating this process that we're after here so uh right here we'll say for name and names and now we want to add each one to that HTML output so we'll say HTML output plus okay and this is another FST string here so first we're putting in a new line with a back sln and then we're putting in a tab with the back SLT and then the list item is this Li tag here and then we're putting the name this is the python um variable that we're using it's going to substitute this out and then in HTML these Ford slashes close out an HTML element so we're closing out that list item okay and now after we have all those list items there outside our loop we're going to have to close off the entire list alog togethere so we'll say HTML output plus equals and we'll close off that list item with a forge slul and let's not forget to put in a new line there at the beginning just to clean up how this prints out okay so now let's print this out and see what everything looks like so I will raise our uh output here okay so this is looking good so at this point I think that this is the exact output that we wanted so at this point we could be done but I wanted to show you one more thing um I told you that I'd show you one more way to parse the CSV file that I prefer more than using the reader method and what I prefer is to use the dictionary reader and we can use this by saying dict reader now the difference between the reader and the dictionary reader is that the dictionary reader turns each line into a dictionary instead of a list and the dictionary has each field as a key and then the data as the values so let me just Loop through and print out these values so you can see what this looks like so first let me just comment out these lines where we're doing all of our looping and everything and I'm also going to comment out the HTML output for now and we're going to see what the output of this dictionary reader looks like and to do this we'll say for item in CSV data and then we will just print out each item so let's run that okay now at first glance this looks a little messy uh especially since my text is so large here but each of these lines is an ordered dictionary so the first line with the field names is no longer here now those are being used as keys for the dictionaries so the first uh throwaway value is still here as far as this being the description of the reward line so this first order dictionary here is our first line and if we look at our second item now this is our first person because we can see this is that uh John do person so now instead of accessing index zero for the first name and index one for the last name now we can access those directly through the first name and last name keys and I think that is a lot more readable for anyone looking at your code so now to get this working again uh we're going to get rid of this Loop where we just printed everything out and now we'll uncommon out all of the uh logic here okay and now that the headers are no longer included in the output uh we only want to skip over that one first value and let's see and we can fix our comment here and just say we don't want first line of bad data and now instead of using index where we access the items we can now use the keys of first name and last name so now we're going to say if the first name is equal to no reward and then we want to append the first name and append the last name okay and now let's go down here and uncommon out this HTML output and see if this works so now I'll go ahead and rerun this and if we scroll up we have Maggie Jefferson here at the bottom and if we scroll up we can see that there's still 30 contributors and John do is our first one so that seems to be correct okay so it looks like our results are good uh so it took a little while to write the script but now it's going to save a lot a ton of time by automating this in the future and will also prevent me from making any mistakes so one reason that I like to show you all these quick scripts that can automate a repetitive task is just to show how you can save a lot of time by writing a very simple script I mean this script here is only 26 lines and it's going to save us a lot of time and I also want to show that you don't need to overthink these oneoff scripts too much uh so I could probably come into the script and add error checking and also some kind of objectoriented approach to this but for what I want to use the script for I really don't need it to be overly complicated so if you have some problems that you think that you can automate then just give it a shot and don't think too much about how performant or clean everything is um it's just a great way to learn is uh just by doing and experimenting now if any of you are uh interested in a more detailed look at parsing CSV file files and writing CSV files then I am putting together a video specifically on reading and writing CSV files that I'm going to record very soon so be on the lookout for that okay so I think that is going to do it for this video I hope that you all found it useful and if anyone has 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 then there are several ways you can do that the easiest way is 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

---
*Источник: https://ekstraktznaniy.ru/video/12577*