python - 如何解析在 python 中要求凭据的 XML 文件
问题描述
有人可以帮忙吗,我想用我的代码解析一个要求身份验证(用户、密码)的 XML 文件。有人可以帮助我在我的代码中使用或编辑什么,以便我的代码可以解析 svn https 链接吗?
import urllib.request as url
import xml.etree.ElementTree as ET
xmlfile=url.urlopen(r'file:///D:/Python/user.html')
def fileparse(xmlfile):
tree=ET.parse(xmlfile)
root=tree.getroot()
#print(root.tag)
users={root.get("name"):[]}
for item in root.findall("client"):
users[root.get("name")].append(item.get("name"))
return users
k1=input("user or client, please mention:")
if k1=='user':
k10=input("Enter the user id you want to search for:")
d1=fileparse(xmlfile)
for k,v in d1.items():
if k==k10:
print(k,v)
elif k1=='client':
c10=input("Enter the client name you want to search for:")
d1=fileparse(xmlfile)
for k,v in d1.items():
if c10 in v:
print(k)
else:
print("sorry please check your input values")
解决方案
我认为您的代码中存在缩进错误。
users 和 for 循环应该存在于要执行的函数中。
import urllib.request as url
import xml.etree.ElementTree as ET
xmlfile = url.urlopen(r'file:///D:/Python/user.html').read()
def fileparse(xmlfile):
tree = ET.parse(xmlfile)
root = tree.getroot()
# print(root.tag)
users = {root.get("name"): []}
for item in root.findall("client"):
users[root.get("name")].append(item.get("name"))
return users
print(fileparse(xmlfile))
推荐阅读
- sql-server - 连接表,其中表名是另一个表中的值
- c# - C#在GUI中将文本文件逐个字符格式化为二维数组
- triggers - 如何创建逻辑应用自定义连接器轮询触发器?
- azure - 使用 Azure Function 作为流分析的输出:连接测试失败
- php - 我想更改上传图片的名称
- vb.net - 如何在vb中计算两次之间的时间差
- laravel - PHPUnit测试找不到工厂
- swift - 编写谓词以建立具有核心数据关系的计数数组
- amazon-web-services - 如何在 AppSync / Amplify 中过滤非标量类型
- python-3.x - python3 multiprocessing.Pool with maxtasksperchild=1 不会终止