为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

红糖糍粑 2023-03-16 16:27:07
我无法为我正在编写的程序提出解决方案。我正在阅读 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 dataprint("""%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 = junitartifactId = junitversion = 3.8.1..groupId = javax.transaction artifactId = jtaversion = 1.0.1B
查看完整描述

1 回答

?
月关宝盒

TA贡献1772条经验 获得超5个赞

您可以使用f-strings执行此操作:

for groupId, artifact in dependencyInfo.items():

    artifactId = artifact["artifactId"]

    version = artifact["version"]


    print(f"groupId = {groupId}")

    print(f"artifactId = {artifactId}")

    print(f"version = {version}")

    print()

请注意,您dependencyInfo有一个小错误,此条目:'slf4j-api': {'groupId': 'org.slf4j', 'type': 'jar'}需要artifactId在正文中代替并groupId作为键。


为了适应 wheregroupId和artifactId可以切换,除了 等其他字段type,您可以使用这个:


for dependencyId, info in dependencyInfo.items():

    additionalInfo = {}


    groupId = None

    artifactId = None


    for infoName, infoValue in info.items():

        if infoName == "artifactId":

            artifactId = info["artifactId"]

            groupId = dependencyId

        elif infoName == "groupId":

            artifactId = dependencyId

            groupId = info["groupId"]

        else:

            additionalInfo[infoName] = infoValue


    if groupId:

        print(f"groupId = {groupId}")


    if artifactId:

        print(f"artifactId = {artifactId}")


    for infoName, infoValue in additionalInfo.items():

        print(f"{infoName} = {infoValue}")


    print()

您的结果输出dependencyInfo:


groupId = junit

artifactId = junit

version = 3.8.1


groupId = org.hibernate

artifactId = ejb3-persistence

version = 1.0.1.GA


groupId = javax.sql

artifactId = jdbc-stdext

version = 2.0


groupId = javax.transaction

artifactId = jta

version = 1.0.1B


groupId = mysql

artifactId = mysql-connector-java

version = 5.1.14


groupId = org.slf4j

artifactId = slf4j-api

type = jar


groupId = org.slf4j

artifactId = slf4j-simple

version = 1.6.1

如果两者都不存在,或者两者同时存在于体内,这将产生意想不到的groupId结果artifactId。


查看完整回答
反对 回复 2023-03-16
  • 1 回答
  • 0 关注
  • 99 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信