[英]AttributeError: 'list' object has no attribute 'items' - Python
Im attempting to print out my list in formatted way.我试图以格式化的方式打印出我的列表。 When I try to parse the list with.item()s it returns AttributeError: 'list' object has no attribute 'items' Nothing ive tried so far has worked.
当我尝试使用.item()s 解析列表时,它返回AttributeError: 'list' object has no attribute 'items'到目前为止我没有尝试过任何工作。
import xmltodict
from collections import defaultdict
dependencyInfo = defaultdict(list)
with open("pom.xml") as f:
parse_ = xmltodict.parse(f.read()).get('project', {})
for d in parse_.get("dependencies", {}).get("dependency", []):
dependencyInfo[d['groupId']].append(
{"artifactId": d['artifactId'], 'version': d['version']})
for dependencyId, info in dependencyInfo.items():
additionalInfo = {}
for infoName, infoValue in info.items():
if groupId:
print(f"groupId = {groupId}")
if artifactId:
print(f"artifactId = {artifactId}")
if version:
print(f"version = {version}")
print()
Any help would be great.任何帮助都会很棒。
dependencyInfo
is a dictionary whose values are a list of dictionaries. dependencyInfo
是一个字典,其值是一个字典列表。 So you will have to get a value from the dependencyInfo
object and the enumerate over the list of dictionaries.因此,您必须从
dependencyInfo
object 和字典列表中的枚举中获取一个值。
dependencyInfo = defaultdict(list)
dependencyInfo['a'].append({"artifactId": 1, 'version': 1})
dependencyInfo['a'].append({"artifactId": 2, 'version': 2})
# Iterate over the dict
for dependencyId, info_list in dependencyInfo.items():
# Iterate over the value which is a list of dicts
for info in info_list:
# Iterate over each dict
for infoName, infoValue in info.items():
print (f"{infoName} = {infoValue}")
Output Output
artifactId = 1
version = 1
artifactId = 2
version = 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.