为循环插入键和值

2024-06-16 13:48:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从本地主机网站上获取一些数据

#scrapy shell localhost.aspx

for i in response.xpath('//*[text()="Core Units"]/parent::*/parent::*/parent::*/div'):
    i.xpath('.//text()').extract()

这是输出

['Core Units']
['AB43342', 'Identify learning objectives']
['Elective Units']
['AB43343', 'Engage with texts for personal purposes']
['AB43344', 'Engage with texts for learning purposes']
['AB43345', 'Engage with texts for employment purposes']
['AB43346', 'Engage with texts to participate in the community']
['Extra Units']
['AB43348', 'Create  texts for personal purposes']
['AB43349', 'Create  texts for learning purposes']
['AB43350', 'Create  texts for employment purposes']

我想创建一个字典,如下所示:

di={'Core Units':['Code:AB4334 desc: Identify learning objectives'],
'Elective Units':['Code: AB43343 desc: Engage with texts for personal purposes',
'Code: AB43344 desc:Engage with texts for learning purposes',
...,]
'Extra Units': ['Code: AB43348 desc: Create  texts for personal purposes',
...]
}

我不知道可能出现的键是什么,所以我不能创建一个空字典并开始填充它,我必须从for循环中获取它们


Tags: incoreforcreatewithcodeengagexpath
2条回答

免责声明:使用Python 3.6中的^{}格式

下面是一些对给定信息有帮助的内容

inp = [['Core Units'],
       ['AB43342', 'Identify learning objectives'],
       ['Elective Units'],
       ['AB43343', 'Engage with texts for personal purposes'],
       ['AB43344', 'Engage with texts for learning purposes'],
       ['AB43345', 'Engage with texts for employment purposes'],
       ['AB43346', 'Engage with texts to participate in the community'],
       ['Extra Units'],
       ['AB43348', 'Create  texts for personal purposes'],
       ['AB43349', 'Create  texts for learning purposes'],
       ['AB43350', 'Create  texts for employment purposes']]

from collections import defaultdict
di = defaultdict(list)    # Helpful to just append value to new key in dict
unit = ''
for line in inp:
    if len(line) == 1:
        unit = line[0]    # Sets the current unit (dict key) for upcoming lines
    else:
        di[unit].append(f"Code:{line[0]} desc: {line[1]}")  # Adds line to unit
print(di)

产出:

{'Core Units':     ['Code:AB43342 desc: Identify learning objectives'],  
 'Elective Units': ['Code:AB43343 desc: Engage with texts for personal purposes',  
                    'Code:AB43344 desc: Engage with texts for learning purposes',  
                    'Code:AB43345 desc: Engage with texts for employment purposes',  
                    'Code:AB43346 desc: Engage with texts to participate in the community'],  
 'Extra Units': ['Code:AB43348 desc: Create  texts for personal purposes',  
                 'Code:AB43349 desc: Create  texts for learning purposes',  
                 'Code:AB43350 desc: Create  texts for employment purposes']}

试试这个:

result = {}
for i in response.xpath('//*[text()="Core Units"]/parent::*/parent::*/parent::*/div'):
    line=i.xpath('.//text()').extract()
    if len(line) == 1 :
        last_key = line[0]
        result[last_key] = []
    else :
        result[last_key].append("Code:" + line[0] + " desc: " + line[1])

相关问题 更多 >