从文件中读取字典后打印多个值

我无法为我正在编写的程序提出解决方案。我正在阅读 pom 文件中的礼仪,我将它们保存到字典中。将它们保存到字典后,我想以特定方式输出。如下:


我如何解析 pom 文件的代码:


for dep in depend:

    infoList = []

    counter += 1

    for child in dep.getchildren():

        infoList.append(child.tag.split('}')[1])

        infoList.append(child.text)


    #list where data is being stored

    dependencyInfo[infoList[1]].update({infoList[2] : infoList[3],infoList[4] : infoList[5]})

                   

#print statement of all the data

print("""%i Dependency where found in %s's pom file.""" % (counter,projectName))

print(dependencyInfo)


输出是


11 Dependency where found in my-application1's pom file.

defaultdict(<class 'dict'>, {'junit': {'artifactId': 'junit', 'version': '3.8.1'}, 'org.hibernate': {'artifactId': 'ejb3-persistence', 'version': '1.0.1.GA'}, 'javax.sql': {'artifactId': 'jdbc-stdext', 'version': '2.0'}, '

javax.transaction': {'artifactId': 'jta', 'version': '1.0.1B'}, 'mysql': {'artifactId': 'mysql-connector-java', 'version': '5.1.14'}, 'slf4j-api': {'groupId': 'org.slf4j', 'type': 'jar'}, 'org.slf4j': {'artifactId': 'slf4j

-simple', 'version': '1.6.1'}})

现在我想按如下方式重新排列数据


groupId = junit

artifactId = junit

version = 3.8.1

.

.

groupId = javax.transaction 

artifactId = jta

version = 1.0.1B


红糖糍粑
浏览 104回答 1
1回答

月关宝盒

您可以使用f-strings执行此操作:for groupId, artifact in dependencyInfo.items():&nbsp; &nbsp; artifactId = artifact["artifactId"]&nbsp; &nbsp; version = artifact["version"]&nbsp; &nbsp; print(f"groupId = {groupId}")&nbsp; &nbsp; print(f"artifactId = {artifactId}")&nbsp; &nbsp; print(f"version = {version}")&nbsp; &nbsp; print()请注意,您dependencyInfo有一个小错误,此条目:'slf4j-api': {'groupId': 'org.slf4j', 'type': 'jar'}需要artifactId在正文中代替并groupId作为键。为了适应 wheregroupId和artifactId可以切换,除了 等其他字段type,您可以使用这个:for dependencyId, info in dependencyInfo.items():&nbsp; &nbsp; additionalInfo = {}&nbsp; &nbsp; groupId = None&nbsp; &nbsp; artifactId = None&nbsp; &nbsp; for infoName, infoValue in info.items():&nbsp; &nbsp; &nbsp; &nbsp; if infoName == "artifactId":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; artifactId = info["artifactId"]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; groupId = dependencyId&nbsp; &nbsp; &nbsp; &nbsp; elif infoName == "groupId":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; artifactId = dependencyId&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; groupId = info["groupId"]&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; additionalInfo[infoName] = infoValue&nbsp; &nbsp; if groupId:&nbsp; &nbsp; &nbsp; &nbsp; print(f"groupId = {groupId}")&nbsp; &nbsp; if artifactId:&nbsp; &nbsp; &nbsp; &nbsp; print(f"artifactId = {artifactId}")&nbsp; &nbsp; for infoName, infoValue in additionalInfo.items():&nbsp; &nbsp; &nbsp; &nbsp; print(f"{infoName} = {infoValue}")&nbsp; &nbsp; print()您的结果输出dependencyInfo:groupId = junitartifactId = junitversion = 3.8.1groupId = org.hibernateartifactId = ejb3-persistenceversion = 1.0.1.GAgroupId = javax.sqlartifactId = jdbc-stdextversion = 2.0groupId = javax.transactionartifactId = jtaversion = 1.0.1BgroupId = mysqlartifactId = mysql-connector-javaversion = 5.1.14groupId = org.slf4jartifactId = slf4j-apitype = jargroupId = org.slf4jartifactId = slf4j-simpleversion = 1.6.1如果两者都不存在,或者两者同时存在于体内,这将产生意想不到的groupId结果artifactId。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python