0

Я создаю таблицу по Xml файлу на данный момент она выглядит так: введите сюда описание изображения

Подскажите, пожалуйста, как реализовать отображение границ у таблицы, чтобы было видно границы ячеек. Заранее благодарен.

import xml.etree.cElementTree as et
import pandas as pd
def getvalueofnode(node):
    """ return node text or None """
    return node.text if node is not None else None


def main():
    """ main """
    parsed_xml = et.parse("book.xml")
    dfcols = ['author', 'name', 'publich', 'year','page']
    df_xml = pd.DataFrame(columns=dfcols)

    for node in parsed_xml.getroot():
        author=node.find('author')
        name = node.find('name')
        publich = node.find('publich')
        year = node.find('year')
        page = node.find('page')

        df_xml = df_xml.append(
            pd.Series([getvalueofnode(author), getvalueofnode(name), getvalueofnode(publich),
                       getvalueofnode(year),getvalueofnode(page)], index=dfcols),
            ignore_index=True)

    print df_xml

main()
2
  • что вы хотите получить на выходе? HTML или ASCII таблицу? 1 фев 2019 в 10:46
  • @MaxU вообще не принципиально, что проще реализуется
    – Mr.Flatman
    1 фев 2019 в 10:46

1 ответ 1

1

Пример сохранения Pandas DataFrame в HTML:

In [229]: d = pd.DataFrame({'author':['aaa','bbbbbbb','ccccccccc'], 'name':['book 1','book 11','book 111']})

In [230]: d
Out[230]:
      author      name
0        aaa    book 1
1    bbbbbbb   book 11
2  ccccccccc  book 111

In [231]: d.to_html(r'c:\temp\out.html', index=False)

Результат:

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th>author</th>
      <th>name</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>aaa</td>
      <td>book 1</td>
    </tr>
    <tr>
      <td>bbbbbbb</td>
      <td>book 11</td>
    </tr>
    <tr>
      <td>ccccccccc</td>
      <td>book 111</td>
    </tr>
  </tbody>
</table>

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.