我试图解析XML文件中的所有 plugin 标签,并使用我创建的 Plugin 类构建字典。 我真的不理解错误和错误的背景。
当我尝试将.append()
初始化为列表时, plugin_dict
似乎不起作用。同样,当我尝试调用__init__()
之类的plugin = Plugin()
函数时,我得到一个错误,当我已经实现了__init__(self)
函数时, Plugin 需要至少一个参数不需要参数。
这是 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
错了;我从未见过这样的例子。我只是用它来将XML更好地分类为一个对象
如果您认为我也应将其标记为 python-3.x ,请告诉
答案 0 :(得分:1)
您正在尝试在字典上设置属性
self.version = {}
self.version.code = plugin.version.code.string # This won't work
如果您想在字典上设置键,则需要使用以下语法
self.version['code'] = plugin.version.code.string
您已重新定义了类的__init__
方法,python不支持此方法。在您的代码中,您用第二个替换了第一个定义。 Plugin()
将失败,并显示一条消息,提示缺少必需的参数“插件”