Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 试图开发一个解析XML和创建关系表的程序。不断获得;非类型对象没有属性';getitem'&引用;_Python_Xml_Sqlite - Fatal编程技术网

Python 试图开发一个解析XML和创建关系表的程序。不断获得;非类型对象没有属性';getitem'&引用;

Python 试图开发一个解析XML和创建关系表的程序。不断获得;非类型对象没有属性';getitem'&引用;,python,xml,sqlite,Python,Xml,Sqlite,此任务要求基于ITunes播放列表中的XML文件,使用python创建表并添加数据。曲目的ITunes XML数据示例如下: <dict> <key>Major Version</key><integer>1</integer> <key>Minor Version</key><integer>1</integer> <key>Application V

此任务要求基于ITunes播放列表中的XML文件,使用python创建表并添加数据。曲目的ITunes XML数据示例如下:

<dict>
    <key>Major Version</key><integer>1</integer>
    <key>Minor Version</key><integer>1</integer>
    <key>Application Version</key><string>10.2.1</string>
    <key>Features</key><integer>5</integer>
    <key>Show Content Ratings</key><true/>
    <key>Music Folder</key><string>file://localhost/Users/z/Music/iTunes/iTunes%20Media/</string>
    <key>Library Persistent ID</key><string>F343578A04A17962</string>
    <key>Tracks</key>
    <dict>
        <key>86</key>
        <dict>
            <key>Track ID</key><integer>86</integer>
            <key>Name</key><string>Play Your Part (Pt. 1)</string>
            <key>Artist</key><string>Girl Talk</string>
            <key>Album</key><string>Feed The Animals</string>
            <key>Genre</key><string>Mash-up</string>
            <key>Kind</key><string>MPEG audio file</string>
            <key>Size</key><integer>19612331</integer>
            <key>Total Time</key><integer>284865</integer>

主要版本1
小版本1
应用程序版本10.2.1
特点5
显示内容分级
音乐Folderfile://localhost/Users/z/Music/iTunes/iTunes%20Media/
库持久性IDF353578A04A17962
轨道
86
轨道ID86
扮演你的角色(第1部分)
女艺人谈话
喂动物
翻拍
一种mpeg音频文件
尺寸19612331
总时间284865
这是我的密码:

import xml.etree.ElementTree as ET
import sqlite3
connection = sqlite3.connect('tracktable.sqlite')
cur = connection.cursor()

cur.execute('''
CREATE TABLE IF NOT EXISTS Artist (
    id  INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
    name    TEXT UNIQUE
)''')

cur.execute('''
CREATE TABLE IF NOT EXISTS Genre (
    id  INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
    name    TEXT UNIQUE
)''')

cur.execute('''
CREATE TABLE IF NOT EXISTS Album (
    id  INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
    artist_id  INTEGER,
    title   TEXT UNIQUE
)''')

cur.execute('''
CREATE TABLE IF NOT EXISTS Track (
    id  INTEGER NOT NULL PRIMARY KEY 
        AUTOINCREMENT UNIQUE,
    title TEXT  UNIQUE,
    album_id  INTEGER,
    genre_id  INTEGER,
    len INTEGER, rating INTEGER, count INTEGER
)''')

fname = raw_input('Enter File Name:')
if (len(fname)<1): fname = 'Library.xml'

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')
    genre = lookup(entry, 'Genre')
    length = lookup(entry, 'Total Time')
    if name is None or artist is None or album is None:
        continue
    print name,artist,album,genre,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 IGNORE INTO Genre (name)
        VALUES (?)''', (genre, ))
    cur.execute('SELECT id FROM Genre WHERE name =?', (genre, ))
    genre_id = cur.fetchone()[0]

    cur.execute('''INSERT OR REPLACE INTO Track
        (title, album_id, genre_id, len, rating, count)
        VALUES (?,?,?,?,?,?)''',
        (name, album_id, length, genre_id, rating, count))

connection.commit
将xml.etree.ElementTree作为ET导入
导入sqlite3
connection=sqlite3.connect('tracktable.sqlite')
cur=connection.cursor()
当前执行(“”)
如果不存在,则创建表(
id整数非空主键自动递增唯一,
名称文本唯一
)''')
当前执行(“”)
如果不存在,则创建表(
id整数非空主键自动递增唯一,
名称文本唯一
)''')
当前执行(“”)
如果相册不存在,则创建表(
id整数非空主键自动递增唯一,
艺术家id整数,
标题文本唯一
)''')
当前执行(“”)
如果不存在跟踪,则创建表(
id整型非空主键
自动递增唯一,
标题文本唯一,
相册id整数,
类型id整数,
len整数、额定整数、计数整数
)''')
fname=raw_输入('输入文件名:')

如果(len(fname)看起来有些专辑没有风格

我将检查
类型id是否为None
,否则插入空值:

cur.execute('''INSERT OR IGNORE INTO Genre (name)
    VALUES (?)''', (genre, ))
cur.execute('SELECT id FROM Genre WHERE name =?', (genre, ))

records = cur.fetchone()
genre_id = records[0] if records else "UNKNOWN"

看起来有些专辑没有风格

我将检查
类型id是否为None
,否则插入空值:

cur.execute('''INSERT OR IGNORE INTO Genre (name)
    VALUES (?)''', (genre, ))
cur.execute('SELECT id FROM Genre WHERE name =?', (genre, ))

records = cur.fetchone()
genre_id = records[0] if records else "UNKNOWN"