[英]Printing multiple values from a dictionary after reading it from a file
I am having trouble coming up with a solution to a program I am writing.我无法为我正在编写的程序提出解决方案。 I am reading decencies from a pom file after reading them I save them into a dictionary.我正在阅读 pom 文件中的礼仪,我将它们保存到字典中。 After saving them into a dictionary I want to output in a specific way.将它们保存到字典后,我想以特定方式 output 。 As follows:如下:
My code on how I am parsing the pom file:我关于如何解析 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)
The output is 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'}})
Now I want to rearrange the data as follows现在我想重新排列数据如下
groupId = junit
artifactId = junit
version = 3.8.1
.
.
groupId = javax.transaction
artifactId = jta
version = 1.0.1B
You can do this using f-strings :您可以使用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()
Note that your dependencyInfo
has a slight error, this entry: 'slf4j-api': {'groupId': 'org.slf4j', 'type': 'jar'}
needs to have the artifactId
in the body instead and groupId
as the key.请注意,您的dependencyInfo
有一个小错误,此条目: 'slf4j-api': {'groupId': 'org.slf4j', 'type': 'jar'}
needs to have the artifactId
instead and groupId
as the钥匙。
To accommodate for where groupId
and artifactId
could be switched, in addition to other fields like type
, you could use this:为了适应可以切换groupId
和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()
Resulting output for your dependencyInfo
:为您的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
Though this will do unexpected things if neither groupId
or artifactId
is present, or both are present at the same time in the body.尽管如果groupId
或artifactId
都不存在,或者两者同时存在于体内,这将产生意想不到的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.