Help! Python programming example! Movie collection with CSV. Modify the movie co
ID: 3860170 • Letter: H
Question
Help! Python programming example!
Movie collection with CSV. Modify the movie collection lecture example (which also uses humanize.py) so that it uses true CSV files rather than the janky file format it uses currently with | separating fields. (You should only have to change two functions, readMovies and saveMovies, and make them use csv.reader/csv.writer instead of manually splitting/joining.) Make sure that your program still works correctly when there are commas in movie titles!
Once that's done, open this movie spreadsheet (a collection of about 4400 movies sourced from IMDb) and save it to your computer as CSV (Files Download as Comma-separated values) in the same folder as your copy of movie-collection.py. Adjust your main() so that it uses this file instead and verify that your program works! (Note that the first line of the CSV file will be column headers — "Title,Year,Runtime,Rating" — so you should either remove that from your file or skip over it in your readMovies function.)
Explanation / Answer
ne of the founding principles that drove the design of Python was that code is read much more often than it’s written. Once a piece of code is written it may pass through many hands; read by other developers, documentation authors, auditors and testers. Experienced programmers will also tell you that being able to understand your own code, many months or even years after you wrote it, is extremely important.
Under this guiding principle, Python was designed to mimic natural written English as much as possible. One of these design choices was to use whitespace as a delimiter, rather than braces ({}), or BEGINEND type statements used by other languages.
A delimiter is something that defines the beginning or the end of a block of text that belongs together. Consider the following:
We can easily understand each step of this simple (but delicious!) recipe because it’s formatted in a way that all English speakers can understand – relevant information is grouped together in sentences and paragraphs and indentation is used so we can differentiate between the steps of collection of ingredients and preparation of the nachos.
Python treats whitespace in exactly the same way, for example the ingredients written as a Python list may look like:
You will notice that Python lists use brackets ([]) as a delimiter, with the indentation and surrounding whitespace clearly differentiation where the list starts and ends. (More on lists shortly.)
Functions for preparing and cooking the nachos might be written like this:
Now, this is a rather silly example, but I bet that you found it pretty easy to follow – even without quite understanding Python syntax. Our list of ingredients is broken up into one ingredient per line to create our ingredients list and we have used indentation to differentiate between the two Python functions (prepare_nachos and cook_nachos), and the code that belongs to each function.
Here are some real examples that you will see later in the book:
I don’t expect you to understand this code right now, but as you can see, the layout of the code makes it easy to follow without you necessarily understanding exactly what’s going on.
Indentation and intuitive use of whitespace are not the only stylistic conventions designed to make Python code more readable. Python has a complete style guide called PEP8. I strongly encourage you to read this document, absorb what it has to say any try to follow PEP8’s recommendations in all of your programming.
Interactive Interpreter
Python is a scripted language. This means that, instead of having to compile code before it can be run, the Python interpreter runs the Python code you write directly.
An incredibly useful feature of the Python interpreter is that it can be used interactively, simply by typing python at a command prompt. Try this now, you should get an output something like this:
Those three greater-than symbols (>>>) means that Python is in interactive mode and ready for you to input commands. Try these exercises by typing each command at the Python interactive interpreter
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.