Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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创建嵌套dict的列表_Python_List_Dictionary_Beautifulsoup - Fatal编程技术网

python创建嵌套dict的列表

python创建嵌套dict的列表,python,list,dictionary,beautifulsoup,Python,List,Dictionary,Beautifulsoup,我使用beautifulsoup获取XML数据并将其放入DICT数组中。然而,它并没有像预期的那样工作。同样的格言刚刚被添加到列表中。如何在嵌套for循环的正确阶段将正确的dict添加到列表中 打印的列表应如下所示: [OrderedDict([('name', ‘dogs’), ('type', ‘housed’), ('value', ‘123’)]), OrderedDict([('name', ‘cats’), ('type', ‘wild’), ('value', ‘456’)]),

我使用beautifulsoup获取XML数据并将其放入DICT数组中。然而,它并没有像预期的那样工作。同样的格言刚刚被添加到列表中。如何在嵌套for循环的正确阶段将正确的dict添加到列表中

打印的列表应如下所示:

[OrderedDict([('name', ‘dogs’), ('type', ‘housed’), ('value', ‘123’)]),
 OrderedDict([('name', ‘cats’), ('type', ‘wild’), ('value', ‘456’)]),
 OrderedDict([('name', ‘mice’), ('type', ‘housed’), ('value', ‘789’)])]
把它放在一张单子里比放在一张单子里好吗

Here is the XML:
<window>
    <window class="Obj" name="ray" type="housed">
        <animal name="dogs",  value = "123" />
        <species name="sdogs",  value = "s123" />
    </window>
    <window class="Obj" name="james" type="wild">
        <animal name="cats", type="wild", value = "456" />
        <species name="scats", type="swild", value = "s456" />
    </window>
    <window class="Obj" name="bob" type="housed">
        <animal name="mice",  value = "789" />
        <species name="smice",  value = "s789" />
    </window>
</window>

您正在更新词典并将其添加到列表中。结果就是你一次又一次地使用同一本词典。您应该在子循环开始之前创建一个新字典,并在循环之后添加,而不是在内部

我猜是这样的:

import sys
import pprint
from bs4 import BeautifulSoup as bs
from collections import OrderedDict

soup = bs(open("my.xml"),"lxml")
dicty = OrderedDict()
listy = [];
Objs=soup.findAll('window',{"class":"Obj"})
#print Objs
for Obj in Objs:
    Objarr =  OrderedDict()        #### move this down ####
    #I want to add data to the array here:
    for child in Obj.children:
        if child.name is not None:
            if child.name == "variable":
               #Also, adding data to the array here:
                Objarr.update({"name" : Obj['text']})
                Objarr.update({"type" : " matrix”})
                Objarr.update({"value": child['name']})
    listy.append(Objarr)           #### dedent this ####

pprint.pprint(listy)

您正在更新词典并将其添加到列表中。结果就是你一次又一次地使用同一本词典。您应该在子循环开始之前创建一个新字典,并在循环之后添加,而不是在内部

我猜是这样的:

import sys
import pprint
from bs4 import BeautifulSoup as bs
from collections import OrderedDict

soup = bs(open("my.xml"),"lxml")
dicty = OrderedDict()
listy = [];
Objs=soup.findAll('window',{"class":"Obj"})
#print Objs
for Obj in Objs:
    Objarr =  OrderedDict()        #### move this down ####
    #I want to add data to the array here:
    for child in Obj.children:
        if child.name is not None:
            if child.name == "variable":
               #Also, adding data to the array here:
                Objarr.update({"name" : Obj['text']})
                Objarr.update({"type" : " matrix”})
                Objarr.update({"value": child['name']})
    listy.append(Objarr)           #### dedent this ####

pprint.pprint(listy)

查看以下内容以了解您的
objs
包含的内容:

>>> soup = bs(open("my_xml.xml"),"lxml")
>>>
>>> objs = soup.findAll('window',{"class":"Obj"})
>>>
>>> for obj in objs:
...     for child in obj.children:
...         print child
...


<animal name="dogs" type="housed" value="123"></animal>


<animal name="cats" type="wild" value="456"></animal>


<animal name="mice" type="housed" value="789"></animal>


<window>
</window>

查看以下内容以了解您的
objs
包含的内容:

>>> soup = bs(open("my_xml.xml"),"lxml")
>>>
>>> objs = soup.findAll('window',{"class":"Obj"})
>>>
>>> for obj in objs:
...     for child in obj.children:
...         print child
...


<animal name="dogs" type="housed" value="123"></animal>


<animal name="cats" type="wild" value="456"></animal>


<animal name="mice" type="housed" value="789"></animal>


<window>
</window>


请添加一些
Objs
内容的示例以及预期的
listy
。是的,现在这样做:)hist ettanany我添加了返回的列表对象。您还需要添加一些
Objs
content.touche的示例,我看到了这个请求,现在就这样做请添加一些
Objs
内容的示例以及预期的
listy
。现在这样做:)hist ettanany我添加了返回的列表对象。您还需要添加一些
Objs
内容的示例。touche,我看到了那个请求,现在就这样做不,我没有足够的输入来测试它(这就是我写
的原因,我猜是
)。但是原始帖子中的
listy.append
似乎不正确,因为它不断添加相同的OrderedICT,并创建一个包含相同对象的多个实例的列表。我的示例演示了如何(1)创建新的OrderedICT,(2)向其中添加内容,以及(3)将其添加到列表中。从OP复制的答案中有错误。@Nurzhan很可能有错误,但我的评论中突出显示的两个更正解决了这个问题:对真实代码的更改相同(问题中的代码既不完整也不起作用)将解决问题非常感谢,这是正确的答案。我已经更新了我的代码,因为它有巨大的错误,对此表示抱歉。我仍然设法找到了问题。谢谢:)我很高兴它能工作:)关于StackOverflow的一点建议:你不应该修改这个问题,否则这些答案对未来的读者毫无意义。您可以添加一个包含与注释或答案相关的进一步解释的部分。不,我没有足够的输入来测试它(这就是我编写
的原因,我想
)。但是原始帖子中的
listy.append
似乎不正确,因为它不断添加相同的OrderedDict并创建一个包含相同对象的多个实例的列表。我的例子展示了如何(1)创建一个新的OrderedDict,(2)向其中添加内容,以及(3)将其添加到列表中。从OP复制的答案中有错误。@Nurzhan很可能有错误,但我的评论中突出显示的两个更正解决了这个问题:相同的更改应用于实际代码(问题中的代码既不完整也不工作)将解决问题非常感谢,这是正确的答案。我更新了我的代码,因为它有巨大的错误,对此表示抱歉。我仍然设法确定了问题。谢谢:)我很高兴它能工作:)关于StackOverflow的一点建议:你不应该修改问题,否则,这些答案对未来的读者来说毫无意义。你可以添加一个部分,进一步解释与评论或答案相关的内容。谢谢,我不知道你可以用这种方式使用列表。谢谢,我不知道你可以用这种方式使用列表。