We were successful in accessing and printing the e-mail address from every row in the CSV file. Run this code, and you should see the e-mail address of every attendee printed out. All together, as another example, to print the "2nd" (in human-speak) element of a list, you would write:īack to our attendee CSV file, to print out the e-mail address of every attendee, we'll modify the code a bit in order to get: Namely, for a list my_list containing, we would write my_list to access its first element (Python lists are 0-indexed, just as in many other programming languages - see this). In Python, square brackets are used to access an element located at some position ("index") in a list. We can see these lists (corresponding to rows in the attendees CSV file) are made up of three elements, the third being the e-mail address we'd like to use to compare attendees. Yes, it is the list of attendees from the attendees1.csv file! Hooray!Įxtracting Information From a Python CSV FileĮvery row that we are looping overis a list object( csv_f is a list of lists). ['Jackson', 'Thompson', 'Wallace', 'Wright', 'Butler', 'Simpson', 'Mitchell', list should go on and on. You will see that we've added two lines - one is the for loop which defines that for the following indented lines, the row variable should contain each element from the list, and the second line (indented, since we want this line to be part of the for loop) which will print this row variable. Let's print out the rows contained in the CSV file by looping over the csv_f list. To run this program, in your terminal, go into the directory containing the Python program file and the attendees CSV files, and type python new_attendees.pyĪlthough the code above is valid, it's a bit unsatisfying as it does not output any data, making it a bit hard to follow along. ![]() Namely, the csv.reader function which will parse the open file, and return a parsed list of rows. We'll open the first attendance CSV file, store a reference to this open file as f and then pass this reference to the csv module. We're now ready to write the rest of the program. This will become our program's first line: import csv Although Python provides you with a number of built-in modules, you need to explicitly declare which modules you'll be using. We will need a few things to get started: first, since we will be using the csv module in our code, we'll need to let Python know about this. ![]() The module in question is called, simply, csv. In the case of CSV files, we'll make use of a module built-in to Python which will simplify their parsing. Python allows you to open text files such as these and read their content, either all at once, or line-by-line. Place them in the same directory where your program file, new_attendees.py, lives. Go ahead and download these files to your computer. We'll be using the following example CSV data files (all attendee names and emails were randomly generated): attendees1.csv and attendees2.csv. py extension is typical of Python program files. Once you've seen the interpreter answer back, you can exit it by typing exit() and pressing Enter.Ĭreate a new text file in your favorite editor and give it a sensible name, for instance new_attendees.py. This is great! Try out some basic arithmetics, for instance typing in 2 * 3 and pressing the Enter key. ![]() Once you've got the Python executable running, you should see a line beginning with three greater-than signs. My recommendation would be to get the latest 2.7 version (2.7.6, for instance). Windows users should follow this article to install it. ![]() do you have Python installed on your computer? If you're running under Mac OS X, open the Terminal application from the Utilities directory and type python Environment setup for Python CSV File Reading You would like to know which attendees attended the second bash, but not the first. You have CSV (comma-separate values) files for both years listing each year's attendees. Here is an example situation: you are the organizer of a party and have hosted this event for two years.
0 Comments
Leave a Reply. |