gpt4 book ai didi

python : save dictionary into list

转载 作者:太空宇宙 更新时间:2023-11-03 18:14:33 25 4
gpt4 key购买 nike

我有这样的代码:

item = CitytalkItem()    #scrapy item 
event = self.driver.find_elements_by_xpath("...")
for d in event:
event_date = d.find_element_by_xpath("td[1]")
event_time_type = d.find_element_by_xpath("td[2]")
event_locate = d.find_element_by_xpath("td[3]")

event_date_c = event_date.text
event_time_type_c = event_time_type.text
event_locate_c = event_locate.text
#save like {"event_date": "0808", "event_tim_type": AM "event_locate": "LA"}

我想将其保存到列表中形式如下:

 item['event']: 
[{"event_date": "0808", "event_tim_type": AM "event_locate": "LA"},
{"event_date": "0708", "event_tim_type": PM "event_locate": "BA"},
{"event_date": "0810", "event_tim_type": AM "event_locate": "LA"}, ]

请教我
谢谢

最佳答案

您可以使用 list comprehension :

events = self.driver.find_elements_by_xpath("...")
item['event'] = [{'event_date': event.find_element_by_xpath("td[1]").text,
'event_time_type': event.find_element_by_xpath("td[2]").text,
'event_locate': event.find_element_by_xpath("td[3]").text}
for event in events]

关于 python : save dictionary into list,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25214738/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com