[英]Python list in a dict in a list
I am new to Python please dont be too aggresive if my question is stupid.我是 Python 的新手,如果我的问题很愚蠢,请不要太激进。 I need a list that contains dict, in this dict i have 2 keys,in first key i got simple text, at second i need to have a list of dict (also 2 keys).
我需要一个包含 dict 的列表,在这个 dict 中我有 2 个键,在第一个键中我得到简单的文本,第二个我需要一个 dict 列表(也是 2 个键)。 What i need is:
我需要的是:
daysandlessons=[]
for day in schedule:
daysandlessons.append({'day': day.text})
for lesson in day:
daysandlessons['lessons'].append({'lesson':lesson.text),
'lessonHour':lesson.get_hour]
})
The real code looks like this:真正的代码如下所示:
for line in tr[1:]:
daysandlessons.append({'day': line.find('th').get_text(separator='\n')})
for lesson in line.findAll('td'):
daysandlessons['lessons'].append({
'lesson':lesson.get_text(separator='\n'),
'lessonWitdth':lesson['colspan']
})
I am trying to scrape /parse schedule from university site using BeautifullSoup我正在尝试使用 BeautifullSoup 从大学网站上抓取/解析时间表
So i am trying to create a second key 'lessons' and append there all lessons and 'lessonsHours'.所以我正在尝试创建第二个关键的“课程”和 append,所有课程和“课程时间”。 This code doesnt work .
此代码不起作用。 Please tell me what i am doing wrong, and second how to access all values.
请告诉我我做错了什么,其次如何访问所有值。
Example:例子:
Monday = daysandlessons[0]['day']
Mondaysecondlesson = daysandlessons[0]['lessons'][1]['lesson']
I found solution, here it is:我找到了解决方案,这里是:
daysandlessons=[]
for day in schedule:
temp=[]
for lesson in day:
temp.append({'lesson':lesson.get_text(),'hour':lesson.get_hour()})
daysandlessons.append({'day':day.get_text(),'lessons':temp})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.