亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

紅糖糍粑 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 關注
  • 122 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號