簡體   English   中英

從文件中讀取字典后打印多個值

[英]Printing multiple values from a dictionary after reading it from a file

我無法為我正在編寫的程序提出解決方案。 我正在閱讀 pom 文件中的禮儀,我將它們保存到字典中。 將它們保存到字典后,我想以特定方式 output 。 如下:

我關於如何解析 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)

output 是

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

您可以使用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'} needs to have the artifactId instead and groupId as the鑰匙。

為了適應可以切換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生成 output :

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都不存在,或者兩者同時存在於體內,這將產生意想不到的結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM