How to fix 'AttributeError: 'dict' object has no attribute 'code'

How to fix 'AttributeError: 'dict' object has no attribute 'code'

我正在尝试从 XML 文件中解析所有 plugin 标签,并使用 Plugin class 我创造了。 我真的不明白错误和错误的上下文。

当我尝试将 plugin_dict 初始化为列表时,

.append() 似乎不起作用。此外,当我尝试调用 __init__() 函数时,如 plugin = Plugin(),我得到一个错误,即 Plugin 至少需要 1 个参数,而我已经实现了 __init__(self) ] 不需要参数的函数。

这是来自main.py

的代码
from plugin import Plugin
from bs4 import BeautifulSoup

def parse():
    file = open('../data/windows.xml')
    soup = BeautifulSoup(file, 'xml')
    plugins = soup.find_all('plugin')
    plugin_dict = []
    for plugin in plugins:
        plugin_dict.append(Plugin(plugin))
    return plugin_dict

其中 plugin.py 是:

class Plugin(object):
    __name = 'Unknown'
    __vendor = {}
    __vendor_name = 'Unknown, Inc.'
    __vendor_producturl = 'http://www.example.com/product'
    __vendor = [__vendor_name, __vendor_producturl]
    __version = {}
    __version_code = '0.0.0.0'
    __version_release_date = '01-01-1970'
    __version = [__version_code, __version_release_date]
    __properties = {}
    __beta = False
    __vst3 = False
    __x64 = False
    __category = 'synth/effect'
    __properties = {__beta, __vst3, __x64, __category}
    __magnet_uri = {}
    __iss_links = {}
    __download_uri = {}
    __downloads = {}
    __downloads = [__magnet_uri, __iss_links, __download_uri]

    def __init__(self):
        '''Create an empty instance for later use'''
        self.name = str(__name)
        self.version = {}
        self.version.code = str(__version[0])
        self.version.releasedate = str(__version[1])
        self.version = [self.version.code, self.version.releasedate]
        self.vendor = {}
        self.vendor.name = str(__vendor_name)
        self.vendor.producturl = str(__vendor_producturl)
        self.vendor = [self.vendor.name, self.vendor.producturl]
        self.properties = {}
        self.properties.beta = bool(__properties[0])
        self.properties.vst3 = bool(__properties[1])
        self.properties.x64 = bool(__properties[2])
        self.properties.category = str(__properties[3])
        self.properties = [self.properties.beta, self.properties.vst3, self.properties.x64, self.properties.category]
        self.magneturi = list(__magnet_uri)
        self.isslinks = list(__iss_links)
        self.downloaduri = list(__download_uri)
        self.downloads = {}
        self.downloads = [self.magneturi, self.isslinks, self.downloaduri]
        self.product_dict = {}
        self.product_dict = [self.name, self.vendor, self.properties, self.downloads]
        return self.product_dict

    def __init__(self, plugin):
        self.name = plugin['name']
        self.version = {}
        self.version.code = plugin.version.code.string
        self.version.releasedate = plugin.version.releasedate.string
        self.version = [self.version.code, self.version.releasedate]
        self.vendor= {}
        self.vendor.name = plugin.vendor.string
        self.vendor.producturl = plugin.vendor['url']
        self.vendor = [self.vendor.name, self.vendor.producturl]
        self.properties = {}
        self.properties.beta = plugin['beta']
        self.properties.vst3 = plugin['vst3']
        self.properties.x64 = plugin['x64']
        self.properties.category = plugin['category']
        self.properties = [self.properties.beta, self.properties.vst3, self.properties.x64, self.properties.category]
        magnet_uri_dict = {}
        magnet_uri_dict = plugin.find_all('magnet')
        for magnet_uri in magnet_uri_dict:
            self.magneturi.append[magnet_uri.name, magnet_uri.string]
        iss_link_dict = {}
        iss_link_dict = plugin.find_all('iss/*')
        for iss_link in iss_link_dict:
            self.isslinks.append(iss_link.string)
        download_uri_dict = {}
        download_uri_dict = plugin.find_all('download-uri/*')
        for download_uri in download_uri_dict:
            self.downloaduri.append(download_uri.string)
        self.downloads = {}
        self.downloads = [self.magneturi, self.isslinks, self.downloaduri]
        self.product_dict = {}
        self.product_dict = [self.name, self.vendor, self.properties, self.downloads]
        return self.product_dict

windows.xml 中的插件标签如下所示:

    <plugin name="Serum" vst3="false" beta="false" x64="true" category="synth">
        <vendor url="">Xfer Records</vendor>
        <version>
            <code>1.248</code>
            <release-date>13-03-2019</release-date>
        </version>
        <download-uri>
            <zippy>PLACEHOLDER</zippy>
            <openload>PLACEHOLDER</openload>
            <sr-files>PLACEHOLDER</sr-files>
        </download-uri>
        <iss>
            <mirror1>PLACEHOLDER</mirror1>
            <mirror2>PLACEHOLDER</mirror2>
            <mirror3>PLACEHOLDER</mirror3>
        </iss>
        <magnet type="default"></magnet>
    </plugin>

我想我定义错了self.version.something;我从未见过这样的例子。我只是用它来更好地 class 将 XML 化为一个对象


如果您认为我应该将其标记为 python-3.x 也请告诉

您正试图在字典上设置属性

self.version = {}
self.version.code = plugin.version.code.string  # This won't work

如果您想在字典上设置键,您需要使用以下语法

self.version['code'] = plugin.version.code.string

您重新定义了class的__init__方法,python不支持此方法。在您的代码中,您已将第一个定义替换为第二个。 Plugin() 会失败,并显示一条消息说缺少必需参数 "plugin"