简体   繁体   English

AttributeError: 'list' object 没有属性 'items' - Python

[英]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.

相关问题 Python WordCloud遇到AttributeError:'list'对象没有属性'items' - Python WordCloud meet AttributeError: 'list' object has no attribute 'items' AttributeError:'list'对象在scrapy中没有属性'items' - AttributeError: 'list' object has no attribute 'items' in a scrapy AttributeError: 'str' object 没有属性 'items' (python) - AttributeError: 'str' object has no attribute 'items' (python) Python - AttributeError: 'str' 对象没有属性 'items' - Python - AttributeError: 'str' object has no attribute 'items' AttributeError: 'list' object 在 Python 上没有属性 'question' - AttributeError: 'list' object has no attribute 'question' on Python AttributeError:“列表”对象在python中没有属性“显示” - AttributeError: 'list' object has no attribute 'display' in python Python 2:AttributeError:“list”对象没有“strip”属性 - Python 2: AttributeError: 'list' object has no attribute 'strip' AttributeError: 'list' object 没有属性 'lower' - Python - AttributeError: 'list' object has no attribute 'lower' - Python AttributeError: 'list' object 在 Python 中没有属性 'values' - AttributeError: 'list' object has no attribute 'values' in Python AttributeError: 'list' object 在 Python 上没有属性 'encode' - AttributeError: 'list' object has no attribute 'encode' on Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM