将逗号分隔值转换为 Python 字典
Converting the comma separated values to Python dictionary
我正在获取以下格式的 XML 数据
<?xml version="1.0"?>
<localPluginManager>
<plugin>
<longName>Plugin Usage - Plugin</longName>
<pinned>false</pinned>
<shortName>plugin-usage-plugin</shortName>
<version>0.3</version>
</plugin>
<plugin>
<longName>Matrix Project Plugin</longName>
<pinned>false</pinned>
<shortName>matrix-project</shortName>
<version>4.5</version>
</plugin>
</localPluginManager>
使用下面的程序从 XML
中获取 "longName"
和 "version"
import xml.etree.ElementTree as ET
import requests
import sys
response = requests.get(<url1>,stream=True)
response.raw.decode_content = True
tree = ET.parse(response.raw)
root = tree.getroot()
for plugin in root.findall('plugin'):
longName = plugin.find('longName').text
shortName = plugin.find('shortName').text
version = plugin.find('version').text
master01 = longName, version
print (master01,version)
这给了我下面的输出,我想将其转换为字典格式以进一步处理
('Plugin Usage - Plugin', '0.3')
('Matrix Project Plugin', '4.5')
预期输出 -
dictionary = {"Plugin Usage - Plugin": "0.3", "Matrix Project Plugin": "4.5"}
我认为你应该在开始时创建一个字典:
my_dict = {}
然后在循环中给这个字典赋值:
my_dict[longName] = version
假设您将所有元组存储在一个列表中,您可以像这样迭代它:
tuple_list = [('Plugin Usage - Plugin', '0.3'), ('Matrix Project Plugin', '4.5')]
dictionary = {}
for item in tuple_list:
dictionary[item[0]] = item[1]
或者,在 Python 3 中,改为听写理解。
其实很简单。首先,在循环之前初始化字典,然后在获取键值对时添加它们:
dictionary = {}
for plugin in root.findall('plugin'):
...
dictionary[longName] = version # In place of the print call
import xml.etree.ElementTree as ET
import requests
import sys
response = requests.get(<url1>,stream=True)
response.raw.decode_content = True
tree = ET.parse(response.raw)
root = tree.getroot()
mydict = {}
for plugin in root.findall('plugin'):
longName = plugin.find('longName').text
shortName = plugin.find('shortName').text
version = plugin.find('version').text
master01 = longName, version
print (master01,version)
mydict[longName]=version
我正在获取以下格式的 XML 数据
<?xml version="1.0"?>
<localPluginManager>
<plugin>
<longName>Plugin Usage - Plugin</longName>
<pinned>false</pinned>
<shortName>plugin-usage-plugin</shortName>
<version>0.3</version>
</plugin>
<plugin>
<longName>Matrix Project Plugin</longName>
<pinned>false</pinned>
<shortName>matrix-project</shortName>
<version>4.5</version>
</plugin>
</localPluginManager>
使用下面的程序从 XML
中获取"longName"
和 "version"
import xml.etree.ElementTree as ET
import requests
import sys
response = requests.get(<url1>,stream=True)
response.raw.decode_content = True
tree = ET.parse(response.raw)
root = tree.getroot()
for plugin in root.findall('plugin'):
longName = plugin.find('longName').text
shortName = plugin.find('shortName').text
version = plugin.find('version').text
master01 = longName, version
print (master01,version)
这给了我下面的输出,我想将其转换为字典格式以进一步处理
('Plugin Usage - Plugin', '0.3')
('Matrix Project Plugin', '4.5')
预期输出 -
dictionary = {"Plugin Usage - Plugin": "0.3", "Matrix Project Plugin": "4.5"}
我认为你应该在开始时创建一个字典:
my_dict = {}
然后在循环中给这个字典赋值:
my_dict[longName] = version
假设您将所有元组存储在一个列表中,您可以像这样迭代它:
tuple_list = [('Plugin Usage - Plugin', '0.3'), ('Matrix Project Plugin', '4.5')]
dictionary = {}
for item in tuple_list:
dictionary[item[0]] = item[1]
或者,在 Python 3 中,改为听写理解。
其实很简单。首先,在循环之前初始化字典,然后在获取键值对时添加它们:
dictionary = {}
for plugin in root.findall('plugin'):
...
dictionary[longName] = version # In place of the print call
import xml.etree.ElementTree as ET
import requests
import sys
response = requests.get(<url1>,stream=True)
response.raw.decode_content = True
tree = ET.parse(response.raw)
root = tree.getroot()
mydict = {}
for plugin in root.findall('plugin'):
longName = plugin.find('longName').text
shortName = plugin.find('shortName').text
version = plugin.find('version').text
master01 = longName, version
print (master01,version)
mydict[longName]=version