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

2024-06-26 01:41:20 发布

您现在位置:Python中文网/ 问答频道 /正文

对于我正在编写的程序,我很难找到解决方案。 我正在从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

Tags: 文件inorgchild字典versionwherejunit
1条回答
网友
1楼 · 发布于 2024-06-26 01:41:20

可以使用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作为键

为了适应groupIdartifactId可以切换的位置,除了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

尽管如果groupIdartifactId都不存在,或者两者同时存在于身体中,这会产生意想不到的效果

相关问题 更多 >