当print(list_url)在if循环内时可以打印,但在for循环外时却不能打印

-2 投票
0 回答
48 浏览
提问于 2025-04-12 06:10
list_url = []
for i in key_products: #must be for i in products_id:
     #sortir url pour bon id
     if keyValue == products_id[i]['files'][0]['type']:
          list_url_2 = i, products_id[i]['files'][0]['url']
     #créer une liste de tupples avec list url
          list_url.append(list_url_2)
          list_url
print(list_url)
#I can't print list_url, type none, like it doesn't exists
list_url = []
for i in key_products:#must be for i in products_id:
     #sortir url pour bon id
     if keyValue == products_id[i]['files'][0]['type']:
          list_url_2 = i, products_id[i]['files'][0]['url']
     #créer une liste de tupples avec list url
          list_url.append(list_url_2)
          list_url
          print(list_url)
#list_url appends correctly, and print correctly with a type list

我希望在循环外面能拿到我的列表,我之前能做到,但现在不知道为什么不行了。

下面是我抓取的初始Json对象的简短摘录:key_products是一个键的列表,比如:202306098320880;而products_id是一个字典的列表,我想提取的值是在一个字典里,并且有特定的条件。

#if type = video get url

下面的URL都是假的!!只是为了举例说明下面的json:

"products": {
        "202306098320880": {
            "vrm": "KV23BYB",
            "dealer_id": "8819",
            "files": [
                {
                    "stock_reference": "202306098320880",
                    "type": "thumbnail",
                    "content_type": "image/jpeg",
                    "url": "https://on.com/repo/202306098320880/1280x720-thumb-play.jpg"
                },
                {
                    "stock_reference": "202306098320880",
                    "type": "poster",
                    "content_type": "image/jpeg",
                    "url": "https://on.com/repo/202306098320880/1280x720.jpg"
                },
                {
                    "stock_reference": "202306098320880",
                    "type": "video",
                    "content_type": "video/mp4",
                    "url": "https://on.com/repo/202306098320880/1280x720.mp4
                }
            ],
            "images": [],
            "on_images": [],
            "youtube": null
        },
        "202309262364380": {

0 个回答

暂无回答

撰写回答