很难将键添加到嵌套字典,Python

时间:2015-04-09 11:15:31

标签: python python-2.7 dictionary nested

我在嵌套字典上工作,它工作正常,直到我不得不对它进行一些调整。这是我的python代码中的代码:

from io import BytesIO
import urllib2 as net
from lxml import etree
import lxml.html


dict = {}

urlList = []

urlList.append("http://gbgfotboll.se/serier/?scr=table&ftid=57109")
urlList.append("http://gbgfotboll.se/serier/?scr=table&ftid=57108")

for url in urlList:

    request = net.Request(url)
    response = net.urlopen(request)
    data = response.read()


    dom = lxml.html.parse(BytesIO(data))
    #all table rows


    xpatheval = etree.XPathDocumentEvaluator(dom)
    rows = xpatheval('//div[@id="content-primary"]/table[1]/tbody/tr')

    divName = xpatheval('//*[@id="content-primary"]/h1//text()')[0]
    trash, divisionName = divName.rsplit("- ")

    dict[divisionName]= {}

    for id,row in enumerate(rows):
        columns = row.findall("td")

        teamName = columns[0].find("a").text, # Lag
        teamName
        playedGames = columns[1].text, # S
        wins = columns[2].text,
        draw = columns[3].text,
        lost = columns[4].text,
        dif = columns[6].text, # GM-IM
        points = columns[7].text, # P - last column

        dict[divisionName].update({id :{"teamName":teamName, "playedGames":playedGames, "wins":wins, "draw":draw, "lost":lost, "dif":dif, "points":points }})

print dict

我只会发布三行印刷字典来解释我需要做什么。这就是现在打印出来的内容:

{u'Div 3 Mellersta G\xf6taland, herrar': {
  0: {
  'draw': ('0',), 'lost': ('0',), 'playedGames': ('0',), 'dif': ('0',), 'teamName': ('Gunnilse IS',), 'points': ('0',), 'wins': ('0',)},
  1: {
'draw': ('0',), 'lost': ('0',), 'playedGames': ('0',), 'dif': ('0',), 'teamName': (u'G\xf6tene IF',), 'points': ('0',), 'wins': ('0',)}, 
  2: {
'draw': ('0',), 'lost': ('0',), 'playedGames': ('0',), 'dif': ('0',), 'teamName': ('Hestrafors IF',), 'points': ('0',), 'wins': ('0',)}},

u'Div 3 Nordv\xe4stra G\xf6taland, herrar': {
  0: {
'draw': ('0',), 'lost': ('0',), 'playedGames': ('0',), 'dif': ('0',), 'teamName': ('Ahlafors IF',), 'points': ('0',), 'wins': ('0',)},
   1: {
'draw': ('0',), 'lost': ('0',), 'playedGames': ('0',), 'dif': ('0',), 'teamName': (u'Dalen/ Kroksl\xe4tts FF',), 'points': ('0',), 'wins': ('0',)},
   2: {
'draw': ('0',), 'lost': ('0',), 'playedGames': ('0',), 'dif': ('0',), 'teamName': (u'IFK Fj\xe4r\xe5s',), 'points': ('0',), 'wins': ('0',)},



etc...

无论如何我需要做的是在嵌套的dicationary中添加另一个键,所以它看起来像这样:

{u'Div 3 Mellersta G\xf6taland, herrar': {
 0:{ <------- this is added, increases for every main key
  0: {
  'draw': ('0',), 'lost': ('0',), 'playedGames': ('0',), 'dif': ('0',), 'teamName': ('Gunnilse IS',), 'points': ('0',), 'wins': ('0',)},
  1: {
'draw': ('0',), 'lost': ('0',), 'playedGames': ('0',), 'dif': ('0',), 'teamName': (u'G\xf6tene IF',), 'points': ('0',), 'wins': ('0',)}, 
  2: {
'draw': ('0',), 'lost': ('0',), 'playedGames': ('0',), 'dif': ('0',), 'teamName': ('Hestrafors IF',), 'points': ('0',), 'wins': ('0',)}},

u'Div 3 Nordv\xe4stra G\xf6taland, herrar': {
 1:{ <------- this is added, increases for every main key
  0: {
'draw': ('0',), 'lost': ('0',), 'playedGames': ('0',), 'dif': ('0',), 'teamName': ('Ahlafors IF',), 'points': ('0',), 'wins': ('0',)},
   1: {
'draw': ('0',), 'lost': ('0',), 'playedGames': ('0',), 'dif': ('0',), 'teamName': (u'Dalen/ Kroksl\xe4tts FF',), 'points': ('0',), 'wins': ('0',)},
   2: {
'draw': ('0',), 'lost': ('0',), 'playedGames': ('0',), 'dif': ('0',), 'teamName': (u'IFK Fj\xe4r\xe5s',), 'points': ('0',), 'wins': ('0',)},

我试图做的就是这个,我标记了我在上一段代码中所做的改变:

i = 0  # <------- this is added
for url in urlList:

    request = net.Request(url)
    response = net.urlopen(request)
    data = response.read()


    dom = lxml.html.parse(BytesIO(data))
    #all table rows


    xpatheval = etree.XPathDocumentEvaluator(dom)
    rows = xpatheval('//div[@id="content-primary"]/table[1]/tbody/tr')

    divName = xpatheval('//*[@id="content-primary"]/h1//text()')[0]
    trash, divisionName = divName.rsplit("- ")

    dict[divisionName]= {}

    for id,row in enumerate(rows):
        columns = row.findall("td")

        teamName = columns[0].find("a").text, # Lag
        teamName
        playedGames = columns[1].text, # S
        wins = columns[2].text,
        draw = columns[3].text,
        lost = columns[4].text,
        dif = columns[6].text, # GM-IM
        points = columns[7].text, # P - last column

        dict[divisionName].update({i:{id :{"teamName":teamName, "playedGames":playedGames, "wins":wins, "draw":draw, "lost":lost, "dif":dif, "points":points }}})  
  # up here, this is added (i before id)
i = i+1  # <------- this is added
print dict

但它打印出来像这样:

{u'Div 3 Mellersta G\xf6taland, herrar': {
   0: {
     11: {'draw': ('0',), 'lost': ('0',), 'playedGames': ('0',), 'dif': ('0',), 'teamName': (u'V\xe5rg\xe5rda IK ',), 'points': ('0',), 'wins': ('0',)}}},
u'Div 3 Nordv\xe4stra G\xf6taland, herrar': {
  0: 
   {11: {'draw': ('0',), 'lost': ('0',), 'playedGames': ('0',), 'dif': ('0',), 'teamName': (u'\xc4lvsborg FF',), 'points': ('0',), 'wins': ('0',)}}}}

EDIT1

这是我没有尝试添加第三级:原始输出:

Timocins-MacBook-Air:~ timo$ python test.py
{u'Div 3 Mellersta G\xf6taland, herrar': {0: {'draw': ('0',), 'lost': ('0',), 'playedGames': ('0',), 'dif': ('0',), 'teamName': ('Gunnilse IS',), 'points': ('0',), 'wins': ('0',)}, 1: {'draw': ('0',), 'lost': ('0',), 'playedGames': ('0',), 'dif': ('0',), 'teamName': (u'G\xf6tene IF',), 'points': ('0',), 'wins': ('0',)}, 2: {'draw': ('0',), 'lost': ('0',), 'playedGames': ('0',), 'dif': ('0',), 'teamName': ('Hestrafors IF',), 'points': ('0',), 'wins': ('0',)}, 3: {'draw': ('0',), 'lost': ('0',), 'playedGames': ('0',), 'dif': ('0',), 'teamName': (u'Holmalunds IF Alings\xe5s',), 'points': ('0',), 'wins': ('0',)}, 4: {'draw': ('0',), 'lost': ('0',), 'playedGames': ('0',), 'dif': ('0',), 'teamName': (u'IFK Falk\xf6ping FF',), 'points': ('0',), 'wins': ('0',)}, 5: {'draw': ('0',), 'lost': ('0',), 'playedGames': ('0',), 'dif': ('0',), 'teamName': ('IFK Tidaholm',), 'points': ('0',), 'wins': ('0',)}, 6: {'draw': ('0',), 'lost': ('0',), 'playedGames': ('0',), 'dif': ('0',), 'teamName': ('Lerums IS',), 'points': ('0',), 'wins': ('0',)}, 7: {'draw': ('0',), 'lost': ('0',), 'playedGames': ('0',), 'dif': ('0',), 'teamName': (u'Lunden \xd6ver\xe5s BK',), 'points': ('0',), 'wins': ('0',)}, 8: {'draw': ('0',), 'lost': ('0',), 'playedGames': ('0',), 'dif': ('0',), 'teamName': ('Skara FC',), 'points': ('0',), 'wins': ('0',)}, 9: {'draw': ('0',), 'lost': ('0',), 'playedGames': ('0',), 'dif': ('0',), 'teamName': (u'S\xe4vedalens IF',), 'points': ('0',), 'wins': ('0',)}, 10: {'draw': ('0',), 'lost': ('0',), 'playedGames': ('0',), 'dif': ('0',), 'teamName': ('Vara SK ',), 'points': ('0',), 'wins': ('0',)}, 11: {'draw': ('0',), 'lost': ('0',), 'playedGames': ('0',), 'dif': ('0',), 'teamName': (u'V\xe5rg\xe5rda IK ',), 'points': ('0',), 'wins': ('0',)}}, u'Div 3 Nordv\xe4stra G\xf6taland, herrar': {0: {'draw': ('0',), 'lost': ('0',), 'playedGames': ('0',), 'dif': ('0',), 'teamName': ('Ahlafors IF',), 'points': ('0',), 'wins': ('0',)}, 1: {'draw': ('0',), 'lost': ('0',), 'playedGames': ('0',), 'dif': ('0',), 'teamName': (u'Dalen/ Kroksl\xe4tts FF',), 'points': ('0',), 'wins': ('0',)}, 2: {'draw': ('0',), 'lost': ('0',), 'playedGames': ('0',), 'dif': ('0',), 'teamName': (u'IFK Fj\xe4r\xe5s',), 'points': ('0',), 'wins': ('0',)}, 3: {'draw': ('0',), 'lost': ('0',), 'playedGames': ('0',), 'dif': ('0',), 'teamName': (u'IFK \xc5m\xe5l ',), 'points': ('0',), 'wins': ('0',)}, 4: {'draw': ('0',), 'lost': ('0',), 'playedGames': ('0',), 'dif': ('0',), 'teamName': ('IK Virgo',), 'points': ('0',), 'wins': ('0',)}, 5: {'draw': ('0',), 'lost': ('0',), 'playedGames': ('0',), 'dif': ('0',), 'teamName': ('Kungsbacka IF',), 'points': ('0',), 'wins': ('0',)}, 6: {'draw': ('0',), 'lost': ('0',), 'playedGames': ('0',), 'dif': ('0',), 'teamName': ('Melleruds IF',), 'points': ('0',), 'wins': ('0',)}, 7: {'draw': ('0',), 'lost': ('0',), 'playedGames': ('0',), 'dif': ('0',), 'teamName': ('Skoftebyns IF',), 'points': ('0',), 'wins': ('0',)}, 8: {'draw': ('0',), 'lost': ('0',), 'playedGames': ('0',), 'dif': ('0',), 'teamName': ('Vallens IF',), 'points': ('0',), 'wins': ('0',)}, 9: {'draw': ('0',), 'lost': ('0',), 'playedGames': ('0',), 'dif': ('0',), 'teamName': (u'V\xe4nersborgs IF',), 'points': ('0',), 'wins': ('0',)}, 10: {'draw': ('0',), 'lost': ('0',), 'playedGames': ('0',), 'dif': ('0',), 'teamName': (u'V\xe4stra Fr\xf6lunda IF',), 'points': ('0',), 'wins': ('0',)}, 11: {'draw': ('0',), 'lost': ('0',), 'playedGames': ('0',), 'dif': ('0',), 'teamName': (u'\xc4lvsborg FF',), 'points': ('0',), 'wins': ('0',)}}}

这是您的代码:

Timocins-MacBook-Air:~ timo$ python test.py
{u'Div 3 Mellersta G\xf6taland, herrar': {0: {11: {'draw': ('0',), 'lost': ('0',), 'playedGames': ('0',), 'dif': ('0',), 'teamName': (u'V\xe5rg\xe5rda IK ',), 'points': ('0',), 'wins': ('0',)}}}, u'Div 3 Nordv\xe4stra G\xf6taland, herrar': {0: {11: {'draw': ('0',), 'lost': ('0',), 'playedGames': ('0',), 'dif': ('0',), 'teamName': (u'\xc4lvsborg FF',), 'points': ('0',), 'wins': ('0',)}}}}

EDIT2 让我更清楚我需要什么

key1{
  key2{ <-- THIS IS WHAT I WANT TO ADD, ONE KEY2 PER KEY1
   key3{
    item,
    item,
    item,
   }
 }
}

1 个答案:

答案 0 :(得分:1)

i += 1在for循环之外,这意味着值保持为0(因此每次都会覆盖字典中的字段),直到整个for循环结束。