使用 Python 和 Django 从文件中获取值时出错



我需要一些帮助。我正在尝试以表格格式显示值,但使用 Django 和 Python 出现以下错误。错误如下。

raise SyntaxError("invalid predicate")

SyntaxError: invalid predicate

def search(request):
    per=[]
    if request.method == 'POST':
        rname=request.POST.get('rname')
        serch=request.POST.get('searchby')
        tree = ET.parse('roomlist.xml')
        root = tree.getroot()
        if serch==0:
           xyz = './/*[roomname='
           xyz = xyz + rname
           xyz= xyz + ']'
           result=root.findall(xyz)
           for x in result:
                  per.append({'roomname':x.find('roomname').text,'seat':x.find('noseats').text,'project':x.find('projectorscreen').text,'video':x.find('videoconf').text})
        return render(request,'booking/home.html',{'people': per}) 

下面给出了我的观点部分。

首页.html:

{% if people %}
        <table>
            <tr>
                <th>Room Name</th>
                <th>seats</th>
                <th>Projector screen</th>
                <th>Video Conference</th>
            </tr>
        {% for person in people %}
            <tr>
                <td>{{person.roomname}}</td>
                <td>{{person.seat}}</td>
                <td>{{person.project}}</td>
                <td>{{person.video}}</td>
            </tr>
        {% endfor %}
        </table>
    {% else %}
        <p>No Record in the database</p>
    {% endif %}

我需要从下面的.xml文件中获取所有值。

<?xml version="1.0" ?><roomlist>
  <location name="Bangalore">
    <room id="1uy92j908u092">
      <roomname> Aquarius </roomname>
      <noseats> 10 </noseats>
      <projectorscreen>yes</projectorscreen>
      <videoconf>yes</videoconf>
    </room>
  </location>
<location name="Bhubaneswar"><room id="131198912460"><roomname>cottage</roomname><noseats>5</noseats><projectorscreen>Yes</projectorscreen><videoconf>Yes</videoconf></room></location><location name="puri"><room id="509955554930"><roomname>room1</roomname><noseats>10</noseats><projectorscreen>No</projectorscreen><videoconf>Yes</videoconf></room></location></roomlist>

在这里,我需要从文件和显示中搜索所有值。请帮助我。

看起来您正在使用xml模块来解析 xmls。不幸的是,xml模块的findall仅支持 xpath 的子集。

所以我的建议是改用lxml

from lxml import  etree as ET 

然后:

if serch==0:
    xyz = './/room/roomname[text()="{}"]'.format(rname)
    result=root.xpath(xyz)

相关内容

最新更新