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。
添加回答
举报