将 Solr XML 解析为 Python 字典
我是 python 新手,正在尝试将 xml 文档(填充了 solr 实例的文档)传递到 python 字典中。我在尝试实际完成此任务时遇到困难。我尝试过使用 ElementTree 和 minidom,但似乎无法获得正确的结果。
这是我的 XML 结构:
<add>
<doc>
<field name="genLatitude">45.639968</field>
<field name="carOfficeHoursEnd">2000-01-01T09:00:00.000Z</field>
<field name="genLongitude">5.879745</field>
</doc>
<doc>
<field name="genLatitude">46.639968</field>
<field name="carOfficeHoursEnd">2000-01-01T09:00:00.000Z</field>
<field name="genLongitude">6.879745</field>
</doc>
</add>
我需要将其转换为一本字典,如下所示:
doc {
"genLatitude": '45.639968',
"carOfficeHoursEnd": '2000-01-01T09:00:00.000Z',
"genLongitude": '5.879745',
}
我不太熟悉字典的工作原理,但还有一种方法可以将所有“文档”放入一个字典中。
干杯。
I am new to python and am trying to pass an xml document (filled with documents for a solr instance) into a python dictionary. I am having trouble trying to actually accomplish this. I have tried using ElementTree and minidom but I can't seem to get the right results.
Here is my XML Structure:
<add>
<doc>
<field name="genLatitude">45.639968</field>
<field name="carOfficeHoursEnd">2000-01-01T09:00:00.000Z</field>
<field name="genLongitude">5.879745</field>
</doc>
<doc>
<field name="genLatitude">46.639968</field>
<field name="carOfficeHoursEnd">2000-01-01T09:00:00.000Z</field>
<field name="genLongitude">6.879745</field>
</doc>
</add>
And From this I need to turn it into a dictionary that looks like:
doc {
"genLatitude": '45.639968',
"carOfficeHoursEnd": '2000-01-01T09:00:00.000Z',
"genLongitude": '5.879745',
}
I am not too familiar with how dictionaries work but is there also a way to get all the "docs" into one dictionary.
cheers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
输出
其中
xmlstr
为:Output
Where
xmlstr
is:如果在请求参数中添加 wt=python ,Solr 可以返回 Python 字典。要将此文本响应转换为 Python 对象,请使用
ast.literal_eval (text_response)
。这比解析 XML 简单得多。
Solr can return a Python dictionary if you add
wt=python
to the request parameters. To convert this text response into a Python object, useast.literal_eval(text_response)
.This is much simpler than parsing the XML.
使用 ElementTree 的可能解决方案,为了示例,输出格式非常漂亮:
您显示的 XML 文档没有提供区分每个
doc
的方法,因此我认为列表是最好的收集每个字典的结构。事实上,如果您想将每个 doc 数据插入到另一个字典中,当然可以,但您需要为该字典选择合适的键。例如,使用Python为每个对象提供的
id
,您可以这样写:这个例子只是为了让您了解如何使用外部字典。如果您决定走这条路,我建议您找到一个有意义且可用的键,而不是
id
返回的对象的内存地址,该地址在不同的运行中可能会发生变化。A possible solution using ElementTree, with output pretty formatted for sake of example:
The XML document you show does not provide a way to distinguish each
doc
from the other, so I would maintain that a list is the best structure to collect each dictionary.Indeed, if you want to insert each
doc
data into another dictionary, of course you can, but you need to choose a suitable key for that dictionary. For example, using theid
Python provides for each object, you could write:This example is designed just to let you see how to use the outer dictionary. If you decide to go down this path, I would suggest you to find a meaningful and usable key instead of the obejct's memory address returned by
id
, which can change from run to run.将来自外部的任何字符串直接评估到 python 中是有风险的。谁知道里面有什么。
我建议使用 json 接口。像这样的东西:
It's risky to eval any string that comes from the outside directly into python. Who knows what's in there.
I'd suggest using the json interface. Something like: