![]() Now, it's not quite as rich as the data model we built because there's no genres in here. And it happens to know that it's SQL, thank you, Atom, for that. And you'll notice this is a triple-quoted string, so this is just one big, long string here. And then I'm going to do the create table, and I'm doing this all automatically. So I'm going to throw away the artist table, album table, and track table, very similar to the stuff we covered in lecture. And so I'm executing a script, which is a series of SQL commands separated by semicolons. In later things, we won't wipe out the database every time. And in order to sort of Bootstrap this nicely, we are going, because this code is going to run all the time, it's going to run and read all of library docs XML. Now, remember that we have a cursor, which is sort of like a file handle, it's really a database handle, as it were. And so right now, if I'm in my tracks data, that file doesn't exist, but what we'll see is this is actually going to create it. And in this, once we run this, you'll see that that file will exist. And then we're going to make a database connection. And so we import that, we're going to import sqlite3 because we want to talk to the database. And so here's tracks.py, we're going to do some XML. So you export that from iTunes, and you can use my file or you can use your file. And it's a weird, weird format, but we'll write some Python to be able to read it. This is a dictionary, then another dictionary, then another dictionary, and then the key for that dictionary. This, to me, could be so nice if it was JSON because it's really a list of dictionaries. And so it's like, I'm going to make a dictionary that has this, then a dictionary within a dictionary. And this XML that Apple produces out of iTunes is a little weird, in that it's kind of key values, and so you see key value pairs. So we're going to actually pull all this data. And so this is also going to review how to read XML. It's got this library.xml file, which you can export from your, if you have iTunes, you can export this, or you can just play with my iTunes. There is also tracks.zip that you can grab that has these two things. So the code that I'm going through is in tracks. And this is really about SQL and Python, so I'll go through this in some detail. So a lot of the lectures that I give in this database chapter are just about SQL. So the code we're going to look at is from the database chapter, and we're going to look at tracks.py. And if you want to follow along with the code, the sample code is here in the materials of my Python for Everybody website. We're going to do some code walkthroughs actually running some code. Print('That album already exists.Hello, everybody, welcome to Python for Everybody. Take care to check that an artist exists in the dictionary before adding an album, and that an album exists before adding a song. Then add commands for adding albums and songs. First, add a command that adds an artist name to the music dictionary. Extend the program such that a user can add artists, albums, and songs to the library. The following program uses nested dictionaries to store a small music library. The following example demonstrates a program that uses 3 levels of nested dictionaries to create a simple music library. ZyDE 9.15.1: Nested dictionaries example: Music library. If you can think of anything better, let everyone know. I wanted to help, so here is my answer since it was a little hard for me. So, I wanted to answer this question for those who could not answer this question.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |