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. After saving them into a dictionary I want to output in a specific way. As follows:
My code on how I am parsing the pom file:
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
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 :
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.
To accommodate for where groupId
and artifactId
could be switched, in addition to other fields like type
, you could use this:
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
:
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.