打印字典?

时间:2017-01-10 21:35:20

标签: python python-3.x dictionary

我正在尝试使用以下方法打印Python3字典:

    b_result_page = []
    b_result_list = []
    b_result_page_and_list = {}

    #b_result page contains urls like: somesite.com?first=11
    for brp in b_result_page:
        LIST_SELECT = '.b_algo'
        for page_item_result in response.css(LIST_SELECT):
            NAME_SELECT = 'h2 a ::attr(href)'
            yield {
                'name' : page_item_result.css(LIST_SELECT).extract(),
            }
            #b_result_page_and_list is an empty dict, and im trying to append several page_item_result to its key.
            b_result_page_and_list.setdefault(brp, page_item_result)

    for k,v in b_result_page:
        print("key: {}  value:{}\n".format(k, v))

当我尝试通过scrapy runspider scrape_b2.py运行此操作时,出现错误:

    for k,v in b_result_page:
ValueError: too many values to unpack (expected 2)

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:2)

两个问题:

  1. 您想循环遍历b_result_page_and_list

  2. 在字典上循环只会循环其键;你的意思是for k, v in b_result_page_and_list.items():来获取键和值。