本文介紹了xml 文件的根目錄為 NONE 為什么?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
from elementtree import ElementTree as ET
tree= ET.parse(r'N:myinternworkfiles xml of bus systems estonieeebus.xml','r')
root= tree.getroot()
print(root)
現(xiàn)在錯誤在輸出中,因為它沒有給出任何結果
now the error is in output as it is giving none
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="cooking">
<author>Giada De Laurentiis</author>
</book>
</bookstore>
推薦答案
下面的代碼就足夠了.您不需要在開始時打開文件.如果您提供正確的路徑,ET.parse
將為您完成.
The following code is enough. You don't need to open the file at beginning. ET.parse
will do it for you if you provide the correct path.
在您的代碼中,您將庫導入為 ET,然后通過打開文件重新分配相同的變量.所以 ET.parse()
指的是你的文件對象而不是你的 elementtree
庫.
In your code you import the library as ET and then reassign the same variable by opening the file. So ET.parse()
referes to your file object not to your elementtree
library.
import xml.etree.ElementTree as ET
tree= ET.parse('note.xml')
root= tree.getroot()
print(root.tag) # to print root
這篇關于xml 文件的根目錄為 NONE 為什么?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!