簡體   English   中英

在python中解析不是純xml的文件的最佳方法是什么

[英]what is the best way to parse a file which is not pure xml in python

我試圖解析不是純xml的python文件,因為它不是純xml,xml解析器無法解析該文件。

請建議我一個解決方案,我不想讀取具有I / O功能的文件。

<groups>
   <url>
      description = helloz
      <whatis>
         <what_is_that>
            active = yes
            <inside_what>
               <default>
                  <0>
                     tagid = 0

                  </0>
               </default>
            </inside_what>
            <second_list>
               <0>
                  name = do
               </0>
            </second_list>
         </what_is_that>

您可以使用BeautifulSoup嘗試類似的操作。 當您創建BeautifulSoup對象時,它將單獨插入缺少的結束標記。 然后,您可以輕松提取所需的任何內容。

from bs4 import BeautifulSoup

with open('file_name', 'r') as f:
    a = f.read()
    soup=BeautifulSoup(a, 'lxml')
    print soup.find('inside_what')

輸出:

<inside_what>
<default>

                     tagid = 0

                  0&gt;
               </default>
</inside_what>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM