简体   繁体   English

如何从python / Django的xml解析中删除选择字符?

[英]How to remove select characters from xml parse in python / django?

Context 上下文

I am working on a django project and I need to loop through a nested dictionary to print the values 我正在处理django项目,我需要遍历嵌套字典以打印值

Here's the dictionary: 这是字典:

{body{u'@copyright': u'All data copyright Unitrans ASUCD/City of Davis 2015.', u'predictions': {u'@routeTitle': u'A', u'@dirTitleBecauseNoPredictions': u'Outbound to El Cemonte', u'@agencyTitle': u'Unitrans ASUCD/City of Davis', u'@stopTag': u'22258', u'@stopTitle': u'Silo Terminal & Haring Hall (WB)', u'@routeTag': u'A', u'message': [{u'@text': u'Weekend service is running Monday-Wednesday Dec. 28-30.', u'@priority': u'Normal'}, {u'@text': u'The A-line and Z-line do not run on weekends. {body {u'@ copyright':u'所有数据版权Unitrans ASUCD /戴维斯市2015。',u'predictions':{u'@ routeTitle':u'A',u'@ dirTitleBecauseNoPredictions':u'Outbound到El Cemonte',u'@ agencyTitle':u'Unitrans ASUCD /戴维斯市',u'@ stopTag':u'22258',u'@ stopTitle':u'Silo码头和哈林大厅(WB)', u'@ routeTag':u'A',u'message':[{u'@ text':u'周末服务于12月28日至30日星期一至星期三运行。',u'@ priority':u'正常'},{u'@ text':u'A行和Z行不在周末运行。 Use O-line for weekend service.', u'@priority': u'Normal'}]}}} 使用O线进行周末服务。',u'@ priority':u'Normal'}]}}}

I am parsing the dictionary from the following url: http://webservices.nextbus.com/service/publicXMLFeed?command=predictions&a=unitrans&r=A&s=22258 我正在从以下URL解析字典: http : //webservices.nextbus.com/service/publicXMLFeed? command = predictions&a = unitrans&r =A& s= 22258

Problem 1 问题1

I am getting trouble displaying the values of keys with '@' in them using django template tags, for example 我在使用django模板标签显示带有'@'的键的值时遇到麻烦

    {% for i in data%}
      {% i.@copyright %}
    {% endfor %}

This gives an error saying could not parse remainder. 这给出了一个错误,指出无法解析余数。

Problem 2 问题2

One of the values has a nested dictionary in it with square brackets 其中一个值包含一个带有方括号的嵌套字典

[{u'@text': u'Weekend service is running Monday-Wednesday Dec. 28-30.', u'@priority': u'Normal'}, {u'@text': u'The A-line and Z-line do not run on weekends. [{u'@ text':u'Weekend服务正在12月28日至30日的周一至周三运行。',u'@ priority':u'Normal'},{u'@ text':u'A行和Z线不在周末运行。 Use O-line for weekend service.', u'@priority': u'Normal'}] 使用O线进行周末服务。',u'@ priority':u'Normal'}]

I cannot loop through this using for loop template tags 我不能使用for循环模板标签来循环浏览

The solution I have in mind 我想到的解决方案

In order to solve this and make it simpler I am looking to strip the characters '@' , '[' and ']' from the xml, this would leave me with a much simpler dictionary which would be easy to loop through. 为了解决这个问题并使它更简单,我正在寻找从xml中删除字符'@''['']'的方法,这将使我拥有一个更加简单的字典,该字典很容易遍历。

My Python Code Right Now in views.py 我的Python代码现在在views.py中

import xmltodict
import requests
def prediction(request, line, s_id):
    url = "http://webservices.nextbus.com/service/publicXMLFeed?  command=predictions&a=unitrans&r=" + line + "&s=" + s_id
    data = requests.get(url)
    data = xmltodict.parse(data, dict_constructor=dict)
    data_dict = {}
    data_dict["data"] = data
    return render(request, 'routes/predictions.html', data_dict)

What I want to display on page predictions.html 我想在网页预测中显示的内容

Route Tag: A

Message : Weekend Service is running Monday-Wednesday Dec. 28-30.
The A-Line and Z-Line do not run on weekends. use O-Line for weekend service.

Priority: Normal

I would appreciate any inputs on this problem. 对于这个问题,我将不胜感激。 Thank you for your time. 感谢您的时间。

In xmltodict, the '@' symbols are there to indicate attributes of xml nodes, and the '[' and ']' are used to delimit element values that are themselves a list of values. 在xmltodict中,“ @”符号用于指示xml节点的属性,而“ [”和“]”用于定界本身就是值列表的元素值。 (Here, it indicates the 'message' value is itself a list of two message objects). (在这里,它指示'message'值本身就是两个消息对象的列表)。 You can certainly try to read in the dict as raw text and scrape out what you need, but that won't take advantage of the reason most people are importing it to begin with: To organize the data and make it easier to access. 您当然可以尝试将dict作为原始文本阅读,然后抓取所需的内容,但这并不会利用大多数人从一开始就将其导入的原因:组织数据并使其易于访问。

Instead of scraping the text, you can easily craft a template that would just pull the specific values from the dict that you want. 无需抓取文本,您可以轻松地制作一个模板,该模板将从所需的字典中提取特定值。 Your data dict should be structured something like this: 您的数据字典应采用以下结构:

{
    body:
    {
        u'@copyright': u'All data copyright Unitrans ASUCD/City of Davis 2015.',
        u'predictions':
        {
            u'@routeTitle': u'A',
            u'@dirTitleBecauseNoPredictions': u'Outbound to El Cemonte',
            u'@agencyTitle': u'Unitrans ASUCD/City of Davis',
            u'@stopTag': u'22258',
            u'@stopTitle': u'Silo Terminal & Haring Hall (WB)',
            u'@routeTag': u'A',
            u'message':
            [
                {
                    u'@text': u'Weekend service is running Monday-Wednesday Dec. 28-30.',
                    u'@priority': u'Normal'
                },
                {
                    u'@text': u'The A-line and Z-line do not run on weekends. Use O-line for weekend service.',
                    u'@priority': u'Normal'
                }
            ]
        }
    }
}

To get the output you want, create a template tailored for this data and then just insert directly the values you need. 要获得所需的输出,请创建针对该数据量身定制的模板,然后直接插入所需的值。 Something like this: (apologies, I don't know django template syntax exactly) 像这样:(抱歉,我不完全知道Django模板语法)

Route Tag: {{ data_dict.body.predictions.routeTitle }}

Messages :
<ul>
{% for msg in data_dict.body.predictions.message %}
    <li>{{ msg.text }} (Priority: {{ msg.priority }})</li>
{% endfor %}
</ul>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM