Python 如何将字典中的所有元素放入数据库中

Python 如何将字典中的所有元素放入数据库中,python,database,list,dictionary,Python,Database,List,Dictionary,我告诉你我的问题: 首先,, 我有一个python列表字典,如下所示: "links": [{"url": "http://catherineingram.com/biography.html", "type": {"key": "/type/link"}, "title": "Biography"}, {"url": "http://www.youtube.com/watch?v=4lJK9cfXP3c", "type": {"key": "/type/link"}, "title": "In

我告诉你我的问题:

首先,, 我有一个python列表字典,如下所示:

"links": [{"url": "http://catherineingram.com/biography.html", "type": {"key": "/type/link"}, "title": "Biography"}, {"url": "http://www.youtube.com/watch?v=4lJK9cfXP3c", "type": {"key": "/type/link"}, "title": "Interview on Consciousness TV"}, {"url": "http://www.huffingtonpost.com/catherine-ingram/", "type": {"key": "/type/link"}, "title": "Blog on Huffington Post"}]
我的目标是只获取链接的url和标题,并将它们放入数据库中。 目前我只使用url,我做到了:

for record in csv.DictReader(open(INPUT_FILE, 'r'), fieldnames=COLUMNS, delimiter='\t'):
    j = json.loads(record['json'])
 result=[]

    if 'links' in j:
        for link in j['links']:
            result.append({'url': link['url']})
            a=(link['url'])

            print(a)
            links=a


    else:
         links = ''
for record in csv.DictReader(open(INPUT_FILE, 'r'), fieldnames=COLUMNS, delimiter='\t'):
    j = json.loads(record['json'])
    if 'bio' in j and 'value' in j['bio']:
        bio = j['bio']['value']
    else:
        bio = None
    if 'alternate_names' in j:
        for n in j['alternate_names']:
            alternate_names = n
    else:
        alternate_names = None
    if 'uris' in j:
        for n in j['uris']:
            uris = n
    else:
        uris = None




    if 'links' in j:
        for link in j['links']:
            dico=({'url': link['url']})
            print(dico['url'])
            links=dico['url']




    else:
         links = ''

         #   print(n)
            #links_url.append(n['url'])
            #links_title.append(n['title'])
            # links_url.append(n['url'])
            # links_title.append(n['title'])



    c.execute('INSERT INTO AUTHORS VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)',
          [record['key'],
           j.get('name'),
           j.get('eastern_order'),
           j.get('personal_name'),
           j.get('enumeration'),
           j.get('title'),
           bio,
           alternate_names,
           uris,
           j.get('location'),
           j.get('birth_date'),
           j.get('death_date'),
           j.get('date'),
           j.get('wikipedia'),
           links
          ])
db.commit()
结果是:

http://catherineingram.com/biography.html
http://www.youtube.com/watch?v=4lJK9cfXP3c
http://www.huffingtonpost.com/catherine-ingram/
所以这是完美的,我得到了我想要的,但现在的问题是,当我在我的数据库中添加链接时:

links=a
我只得到了数据库中url的最后一个元素,而不是3个url 所以我尝试在我的数据库中有3个url,但我只得到了最后一个。
我希望你能帮我解决我的问题

谢谢你听我说

附言:

如果您想了解更多有关代码的详细信息,请访问:

for record in csv.DictReader(open(INPUT_FILE, 'r'), fieldnames=COLUMNS, delimiter='\t'):
    j = json.loads(record['json'])
result=[]

    if 'links' in j:
        for link in j['links']:
            result.append({'url': link['url']})
            a=(link['url'])

            print(a)
            links=a


    else:
         links = ''

         #   print(n)
            #links_url.append(n['url'])
            #links_title.append(n['title'])
            # links_url.append(n['url'])
            # links_title.append(n['title'])



    c.execute('INSERT INTO AUTHORS VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)',
          [record['key'],
           j.get('name'),
           j.get('eastern_order'),
           j.get('personal_name'),
           j.get('enumeration'),
           j.get('title'),
           bio,
           alternate_names,
           uris,
           j.get('location'),
           j.get('birth_date'),
           j.get('death_date'),
           j.get('date'),
           j.get('wikipedia'),
           links
          ])
db.commit()

正如我在对你的问题的评论中提到的,你有一些缩进问题。我只是猜测一下你想达到什么目的。您还可以将变量赋值给所示代码中从未引用过的变量,因此很可能它们没有在正确的级别声明

for record in csv.DictReader(open(INPUT_FILE, 'r'), fieldnames=COLUMNS, delimiter='\t'):
    j = json.loads(record['json'])
    result=[] # what do you do with variable result? Shiuld this be delclared before the 'for record' statment?
    if 'links' in j:
        for link in j['links']:
            result.append({'url': link['url']})
            a=link['url'] # what do you do with variable a?
            print(a)
            links=a # do you need both variables a and links?
            c.execute('INSERT INTO AUTHORS VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)',
                [record['key'],
                 j.get('name'),
                 j.get('eastern_order'),
                 j.get('personal_name'),
                 j.get('enumeration'),
                 j.get('title'),
                 bio,
                 alternate_names,
                 uris,
                 j.get('location'),
                 j.get('birth_date'),
                 j.get('death_date'),
                 j.get('date'),
                 j.get('wikipedia'),
                 links
                ])
    else:
         links = ''
         #   print(n)
            #links_url.append(n['url'])
            #links_title.append(n['title'])
            # links_url.append(n['url'])
            # links_title.append(n['title'])

db.commit() # should this be moved to following the c.execute statement rather than doing one commit for all the inserts?

上面的代码现在正在使用不同的链接写入具有相同数据位的多行。这会导致数据库不规范。你是不是打算写一行,其中列包含3个链接?这也是一个非规范化数据库的例子。再一次,我只是猜测你所说的“我试图在我的数据库中有3个url,但我只得到最后一个。”

谢谢你的回答

我的目标是把我所有的url放在我的数据库中

所以我做到了:

for record in csv.DictReader(open(INPUT_FILE, 'r'), fieldnames=COLUMNS, delimiter='\t'):
    j = json.loads(record['json'])
 result=[]

    if 'links' in j:
        for link in j['links']:
            result.append({'url': link['url']})
            a=(link['url'])

            print(a)
            links=a


    else:
         links = ''
for record in csv.DictReader(open(INPUT_FILE, 'r'), fieldnames=COLUMNS, delimiter='\t'):
    j = json.loads(record['json'])
    if 'bio' in j and 'value' in j['bio']:
        bio = j['bio']['value']
    else:
        bio = None
    if 'alternate_names' in j:
        for n in j['alternate_names']:
            alternate_names = n
    else:
        alternate_names = None
    if 'uris' in j:
        for n in j['uris']:
            uris = n
    else:
        uris = None




    if 'links' in j:
        for link in j['links']:
            dico=({'url': link['url']})
            print(dico['url'])
            links=dico['url']




    else:
         links = ''

         #   print(n)
            #links_url.append(n['url'])
            #links_title.append(n['title'])
            # links_url.append(n['url'])
            # links_title.append(n['title'])



    c.execute('INSERT INTO AUTHORS VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)',
          [record['key'],
           j.get('name'),
           j.get('eastern_order'),
           j.get('personal_name'),
           j.get('enumeration'),
           j.get('title'),
           bio,
           alternate_names,
           uris,
           j.get('location'),
           j.get('birth_date'),
           j.get('death_date'),
           j.get('date'),
           j.get('wikipedia'),
           links
          ])
db.commit()
但当我这样做时,所有其他元素(生物,替代名称…)都起作用,但没有链接,因为我需要使用其他方法,因为这是一个类似于它的字典列表:

"links": [{"url": "http://catherineingram.com/biography.html", "type": {"key": "/type/link"}, "title": "Biography"}, {"url": "http://www.youtube.com/watch?v=4lJK9cfXP3c", "type": {"key": "/type/link"}, "title": "Interview on Consciousness TV"}, {"url": "http://www.huffingtonpost.com/catherine-ingram/"
目前,我只获取字典中的元素url,并尝试将所有链接的url放入数据库中。当我只有1个url,但有时我有2个或3个url时,它工作得非常完美,当它发生时,只有最后一个url在我的数据库中,而不是其他url。这是我的问题


谢谢

首先,您需要向我们展示缩进的精确程度。当前显示的代码无法编译。更正后,我怀疑您的
c.execute
语句需要缩进,因为现在它与
a=(link['url'])
不在同一级别,可能应该是这样。顺便说一句
a=(link['url'])
中的括号既不起任何作用,也没有任何害处。