как получить конкретные теги XML и отображать в разных таблицах? - PullRequest
0 голосов
/ 01 мая 2019

Учитывая XML-файл, я хочу отобразить в своем браузере таблицу журнала с указанием года и другую таблицу, содержащую конференции (название книги), также с указанием года.

ниже, если формат файла XML

<dblpperson>
 <r>
  <article>
    <author orcid="0000-0001-6062-7524">Meinard</author>   
    <author>Bryan Pardo</author><author>Gautham</author>
    <author>Vesa</author>
    <title>Recent Advances in Music Signal Processing [From the Guest 
    Editors].</title>
    <year>2019</year>
    <journal>IEEE Signal Process. Mag.</journal>
    <ee>https://doi.org/10.1109/MSP.2018.2876190</ee>
  </article> 
 </r>
 <r>
  <article>
    <author>Müller</author>   
    <author>Vesa</author>
    <author>Patricio</author>
    <title>Automatic Drum Transcription.</title>
    <year>2018</year>
    <booktitle>ICASSP</booktitle>
    <ee>https://doi.org/10.1109/MSP.2018.2876190</ee>
   </article> 
 </r>
...

ниже это то, что я пробовал до сих пор

@bottle.route("/authors/<name>/synthesis", method='POST')
...
list_of_journals = []
list_of_conf = []

root = ET.fromstring(data.content)
for publication in root.findall('r'):
    for tags in publication:

        #separate journals from conferences
        attribute = tags.attrib['key'].split('/')
        attribute = attribute[0]
        #print(type(attribute))

        if attribute == 'journals':
            titre_j = tags.find('title').text
            ...
            list_of_journals.append([titre_j, année_j, journal_j])
        elif attribute == 'conf':
            titre_c = tags.find('title').text
            ...
            list_of_conf.append([titre_c, année_c, journal_c])

        table = """
                 <table style="width:80%">
                 <tr>
                 <th>Journal</th>  
                 <th>Year</th>
                 </tr>
                 <tr>
                 <td> """ + str(list_of_journals[0][0]) + """</td>
                 ...

1 Ответ

0 голосов
/ 02 мая 2019

Попробуйте этот код

import xml.etree.ElementTree as ET

XML = '''<dblpperson>
 <r>
  <article>
    <author orcid="0000-0001-6062-7524">Meinard</author>   
    <author>Bryan Pardo</author><author>Gautham</author>
    <author>Vesa</author>
    <title>Recent Advances in Music Signal Processing [From the Guest 
    Editors].</title>
    <year>2019</year>
    <journal>IEEE Signal Process. Mag.</journal>
    <ee>https://doi.org/10.1109/MSP.2018.2876190</ee>
    <booktitle>Other ICASSP</booktitle>

  </article> 
 </r>
 <r>
  <article>
    <author>Muller</author>   
    <author>Vesa</author>
    <author>Patricio</author>
    <title>Automatic Drum Transcription.</title>
    <year>2018</year>
    <journal>IEEE Signal Process. Mag. 123</journal>
    <booktitle>ICASSP</booktitle>
    <ee>https://doi.org/10.1109/MSP.2018.2876190</ee>
   </article> 
 </r></dblpperson>'''


def make_table(headers, rows):
    html = '<table>'
    html += '<tr>'
    html += ''.join(['<th>{}</th>'.format(h) for h in headers])
    html += '</tr>'
    for row in rows:
        html += '<tr>'
        html += ''.join(['<td>{}</td>'.format(d) for d in row])
        html += '</tr>'
    html += '</table>'
    return html


journal_data = []
booktitle_data = []
root = ET.fromstring(XML)
articles = root.findall('.//article')
for article in articles:
    journal_data.append([])
    booktitle_data.append([])
    for child in article.getchildren():
        if child.tag == 'year':
            journal_data[-1].append(child.text)
            booktitle_data[-1].append(child.text)
        elif child.tag == 'booktitle':
            booktitle_data[-1].append(child.text)
        elif child.tag == 'journal':
            journal_data[-1].append(child.text)

print(make_table(['Year', 'Journal'], journal_data))
print(make_table(['Year', 'Booktitle'], booktitle_data))

Вывод

<table><tr><th>Year</th><th>Journal</th></tr><tr><td>2019</td><td>IEEE Signal Process. Mag.</td></tr><tr><td>2018</td><td>IEEE Signal Process. Mag. 123</td></tr></table>
<table><tr><th>Year</th><th>Booktitle</th></tr><tr><td>2019</td><td>Other ICASSP</td></tr><tr><td>2018</td><td>ICASSP</td></tr></table>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...