當我使用命名空間和帶有 f-string 的 XPATH 輸入以在 XML 檔案中進行修改時,我的程式出現錯誤
這作業正常,它顯示了 XML 檔案的所有屬性
for x in root.findall(".//{http://www.github/cliffe/SecGen/scenario}vulnerability"):
print(x.tag,"--->",x.attrib)
但是在這段代碼中,NameError: name 'http' is not defined由于 f-string,當我從用戶那里獲取輸入資料時,它給出了一個錯誤
ch = input('\nEnter Tag you want to change : ')
for x in root.findall(f".//{http://www.github/cliffe/SecGen/scenario}vulnerability[@module_path={ch}]"): #ERROR
print("Tag you want to change ",x.tag,"--->",x.attrib)
changes = str(input('\nEnter Your changed tag : '))
x.attrib['module_path'] = f'{changes}'
uj5u.com熱心網友回復:
由于命名空間 URI由花括號分隔,因此您需要對 f 字串的那部分使用雙花括號。您還需要在謂詞中的屬性值周圍加上引號。
root.findall(f".//{{http://www.github/cliffe/SecGen/scenario}}vulnerability[@module_path='{ch}']")
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/481316.html
