def lookup(d, key):
found = False
for child in d:
if found : return child.text
if child.tag == 'key' and child.text == key :
found = True
return None
Dies ist das vollständige Code -Stück. Auch wenn Sie die Datei Library.xml finden müssen, können Sie diesen Link finden, um weitere Kontext zu erhalten: https://www.py4e.com/code3.zip, um die ZIP -Datei herunterzuladen, und dann können Sie zur Datei der BIBLIBORTE.XML -Datei.
import xml.etree.ElementTree as ET
import sqlite3
conn = sqlite3.connect('trackdb.sqlite')
cur = conn.cursor()
# Make some fresh tables using executescript()
cur.executescript('''
DROP TABLE IF EXISTS Artist;
DROP TABLE IF EXISTS Album;
DROP TABLE IF EXISTS Track;
CREATE TABLE Artist (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
name TEXT UNIQUE
);
CREATE TABLE Album (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
artist_id INTEGER,
title TEXT UNIQUE
);
CREATE TABLE Track (
id INTEGER NOT NULL PRIMARY KEY
AUTOINCREMENT UNIQUE,
title TEXT UNIQUE,
album_id INTEGER,
len INTEGER, rating INTEGER, count INTEGER
);
''')
fname = input('Enter file name: ')
if ( len(fname) < 1 ) : fname = 'Library.xml'
# Track ID369
# NameAnother One Bites The Dust
# ArtistQueen
def lookup(d, key):
found = False
for child in d:
if found : return child.text
if child.tag == 'key' and child.text == key :
found = True
return None
stuff = ET.parse(fname)
all = stuff.findall('dict/dict/dict')
print('Dict count:', len(all))
for entry in all:
if ( lookup(entry, 'Track ID') is None ) : continue
name = lookup(entry, 'Name')
artist = lookup(entry, 'Artist')
album = lookup(entry, 'Album')
count = lookup(entry, 'Play Count')
rating = lookup(entry, 'Rating')
length = lookup(entry, 'Total Time')
if name is None or artist is None or album is None :
continue
print(name, artist, album, count, rating, length)
cur.execute('''INSERT OR IGNORE INTO Artist (name)
VALUES ( ? )''', ( artist, ) )
cur.execute('SELECT id FROM Artist WHERE name = ? ', (artist, ))
artist_id = cur.fetchone()[0]
cur.execute('''INSERT OR IGNORE INTO Album (title, artist_id)
VALUES ( ?, ? )''', ( album, artist_id ) )
cur.execute('SELECT id FROM Album WHERE title = ? ', (album, ))
album_id = cur.fetchone()[0]
cur.execute('''INSERT OR REPLACE INTO Track
(title, album_id, len, rating, count)
VALUES ( ?, ?, ?, ?, ? )''',
( name, album_id, length, rating, count ) )
conn.commit()
Ich kann nicht verstehen, was diese Lookup -Funktion tut: < /p> [code]def lookup(d, key): found = False for child in d: if found : return child.text if child.tag == 'key' and child.text == key : found = True return None [/code] Dies ist das vollständige Code -Stück. Auch wenn Sie die Datei Library.xml finden müssen, können Sie diesen Link finden, um weitere Kontext zu erhalten: https://www.py4e.com/code3.zip, um die ZIP -Datei herunterzuladen, und dann können Sie zur Datei der BIBLIBORTE.XML -Datei.[code]import xml.etree.ElementTree as ET import sqlite3
conn = sqlite3.connect('trackdb.sqlite') cur = conn.cursor()
# Make some fresh tables using executescript() cur.executescript(''' DROP TABLE IF EXISTS Artist; DROP TABLE IF EXISTS Album; DROP TABLE IF EXISTS Track;
CREATE TABLE Artist ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, name TEXT UNIQUE );
CREATE TABLE Album ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, artist_id INTEGER, title TEXT UNIQUE );
CREATE TABLE Track ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, title TEXT UNIQUE, album_id INTEGER, len INTEGER, rating INTEGER, count INTEGER ); ''')
# Track ID369 # NameAnother One Bites The Dust # ArtistQueen def lookup(d, key): found = False for child in d: if found : return child.text if child.tag == 'key' and child.text == key : found = True return None
stuff = ET.parse(fname) all = stuff.findall('dict/dict/dict') print('Dict count:', len(all)) for entry in all: if ( lookup(entry, 'Track ID') is None ) : continue
name = lookup(entry, 'Name') artist = lookup(entry, 'Artist') album = lookup(entry, 'Album') count = lookup(entry, 'Play Count') rating = lookup(entry, 'Rating') length = lookup(entry, 'Total Time')
if name is None or artist is None or album is None : continue
print(name, artist, album, count, rating, length)
cur.execute('''INSERT OR IGNORE INTO Artist (name) VALUES ( ? )''', ( artist, ) ) cur.execute('SELECT id FROM Artist WHERE name = ? ', (artist, )) artist_id = cur.fetchone()[0]
cur.execute('''INSERT OR IGNORE INTO Album (title, artist_id) VALUES ( ?, ? )''', ( album, artist_id ) ) cur.execute('SELECT id FROM Album WHERE title = ? ', (album, )) album_id = cur.fetchone()[0]
Ich habe einen Code für einen Prototyp einer Aktienhandelsplattform geschrieben. Ich entschied mich für einen roten schwarzen Baum, um die Transaktionen zu speichern. Ich schrieb den Code für einen...
Was ist der genaueste Weg, um einen Knoten in einer Baumansicht auf und ab zu bewegen. Ich habe ein Kontextmenü auf jedem Knoten erhalten und der ausgewählte Knoten sollte mit allen Subnoden...
Ich habe gerade meine aktuelle Android-Anwendung aktualisiert, um Java 11 und die Build-Tools 32.0.0 zu verwenden.
Hier sind die Details zu Android Studio, die ich verwende
Android Studio Bumblebee...
Ich erhalte diese Fehlermeldung:
Diese Version versteht nur SDK-XML-Versionen bis 2, es wurde jedoch eine SDK-XML-Datei der Version 3 gefunden. Dies kann passieren, wenn Sie Versionen von Android...