首页 > 解决方案 > TypeError 仅发生在 while 循环中

问题描述

我在此代码中收到 TypeError

import requests
import json

orders_collected = 1
all_orders_collected = False

all_orders = requests.get(
    'API location').json()

while all_orders_collected == False:
    if all_orders[orders_collected - 1]['status'] == "processing":
        orders_collected += 1
        all_orders.append(requests.get('API location next page').json())
    else:
        all_orders_collected = True

for order in all_orders:
    print(order)

输出:

    if all_orders[orders_collected - 1]['status'] == "processing":
    TypeError: list indices must be integers or slices, not str

但是,此行仅在 while 循环内时才会给出此错误。在while循环之外它会工作得很好,我很难理解为什么while循环会改变这个条件。API 的输出是列表中的字典。如果有人能解释为什么会发生这种情况,将不胜感激。

数据示例如下:

[{'id': 5339, 'parent_id': 0, 'status': 'processing', 'currency': 'ZAR', 'version': '5.0.0', 'prices_include_tax': False, 'date_created': '2021-03-04T18:06:28', 'date_modified': '2021-03-04T16:09:20', 'discount_total': '0.00', 'discount_tax': '0.00', 'shipping_total': '3.00', 'shipping_tax': '0.00', 'cart_tax': '0.00', 'total': '228.00', 'total_tax': '0.00', 'customer_id': 1, 'order_key': 'wc_order_g6StHaZMVIuoV', 'billing': {'first_name': 'redacted', 'last_name': 'redacted', 'company': '', 'address_1': 'redacted', 'address_2': '', 'city': 'redacted', 'state': 'redacted', 'postcode': '', 'country': 'redacted', 'email': 'redacted', 'phone': 'redacted'}, 'shipping': {'first_name': 'redacted', 'last_name': 'redacted', 'company': '', 'address_1': 'redacted', 'address_2': '', 'city': redacted', 'state': 'redacted', 'postcode': '', 'country': 'redacted'}, 'payment_method': 'cod', 'payment_method_title': 'Pay With EFT', 'transaction_id': '', 'customer_ip_address': 'redacted', 'customer_user_agent': 'redacted', 'created_via': 'checkout', 'customer_note': '', 'date_completed': '2021-02-20T06:28:43', 'date_paid': '2021-02-20T06:28:43', 'cart_hash': 'redacted', 'number': '5339', 'meta_data': [{'id': 44882, 'key': 'is_vat_exempt', 'value': 'no'}, {'id': 44887, 'key': '_new_order_email_sent', 'value': 'true'}], 'line_items': [{'id': 3159, 'name': 'Salads', 'product_id': 4278, 'variation_id': 4280, 'quantity': 1, 'tax_class': '', 'subtotal': '45.00', 'subtotal_tax': '0.00', 'total': '45.00', 'total_tax': '0.00', 'taxes': [], 'meta_data': [{'id': 45521, 'key': 'bases', 'value': 'Quinoa', 'display_key': 'Bases', 'display_value': 'Quinoa'}, {'id': 45522, 'key': 'proteins', 'value': 'Steak Strips', 'display_key': 'Proteins', 'display_value': 'Steak Strips'}, {'id': 45523, 'key': 'first-component', 'value': 'Mixed Peppers', 'display_key': 'First Component', 'display_value': 'Mixed Peppers'}, {'id': 45524, 'key': 'second-component', 'value': 'Organic Sprouts', 'display_key': 'Second Component', 'display_value': 'Organic Sprouts'}, {'id': 45525, 'key': 'third-component', 'value': 'Carrots', 'display_key': 'Third Component', 'display_value': 'Carrots'}, {'id': 45526, 'key': 'fourth-component', 'value': 'Pumpkin Seeds', 'display_key': 'Fourth Component', 'display_value': 'Pumpkin Seeds'}, {'id': 45527, 'key': 'fifth-component', 'value': 'Feta', 'display_key': 'Fifth Component', 'display_value': 'Feta'}, {'id': 45528, 'key': 'sixth-component', 'value': 'Roast Butternut', 'display_key': 'Sixth Component', 'display_value': 'Roast Butternut'}, {'id': 45529, 'key': 'size', 'value': 'Medium', 'display_key': 'Size', 'display_value': 'Medium'}, {'id': 45607, 'key': '_reduced_stock', 'value': '1', 'display_key': '_reduced_stock', 'display_value': '1'}], 'sku': '', 'price': 45, 'parent_name': 'Salads - SOLD OUT'}, {'id': 3160, 'name': 'Salads', 'product_id': 4278, 'variation_id': 4280, 'quantity': 1, 'tax_class': '', 'subtotal': '45.00', 'subtotal_tax': '0.00', 'total': '45.00', 'total_tax': '0.00', 'taxes': [], 'meta_data': [{'id': 45539, 'key': 'bases', 'value': 'Baby Spinach', 'display_key': 'Bases', 'display_value': 'Baby Spinach'}, {'id': 45540, 'key': 'proteins', 'value': 'Grilled Chicken', 'display_key': 'Proteins', 'display_value': 'Grilled Chicken'}, {'id': 45541, 'key': 'first-component', 'value': 'Feta', 'display_key': 'First Component', 'display_value': 'Feta'}, {'id': 45542, 'key': 'second-component', 'value': 'Carrots', 'display_key': 'Second Component', 'display_value': 'Carrots'}, {'id': 45543, 'key': 'third-component', 'value': 'Pumpkin Seeds', 'display_key': 'Third Component', 'display_value': 'Pumpkin Seeds'}, {'id': 45544, 'key': 'fourth-component', 'value': 'Chickpea and Red beans', 'display_key': 'Fourth Component', 'display_value': 'Chickpea and Red beans'}, {'id': 45545, 'key': 'fifth-component', 'value': 'Roast Butternut', 'display_key': 'Fifth Component', 'display_value': 'Roast Butternut'}, {'id': 45546, 'key': 'sixth-component', 'value': 'Organic Sprouts', 'display_key': 'Sixth Component', 'display_value': 'Organic Sprouts'}, {'id': 45547, 'key': 'size', 'value': 'Medium', 'display_key': 'Size', 'display_value': 'Medium'}, {'id': 45608, 'key': '_reduced_stock', 'value': '1', 'display_key': '_reduced_stock', 'display_value': '1'}], 'sku': '', 'price': 45, 'parent_name': 'Salads - SOLD OUT'}, {'id': 3161, 'name': 'Salads', 'product_id': 4278, 'variation_id': 4280, 'quantity': 1, 'tax_class': '', 'subtotal': '45.00', 'subtotal_tax': '0.00', 'total': '45.00', 'total_tax': '0.00', 'taxes': [], 'meta_data': [{'id': 45557, 'key': 'bases', 'value': 'Quinoa', 'display_key': 'Bases', 'display_value': 'Quinoa'}, {'id': 45558, 'key': 'proteins', 'value': 'Chickpea and red beans', 'display_key': 'Proteins', 'display_value': 'Chickpea and red beans'}, {'id': 45559, 'key': 'first-component', 'value': 'Sugar Snap Peas', 'display_key': 'First Component', 'display_value': 'Sugar Snap Peas'}, {'id': 45560, 'key': 'second-component', 'value': 'Red Onion', 'display_key': 'Second Component', 'display_value': 'Red Onion'}, {'id': 45561, 'key': 'third-component', 'value': 'Carrots', 'display_key': 'Third Component', 'display_value': 'Carrots'}, {'id': 45562, 'key': 'fourth-component', 'value': 'Pumpkin Seeds', 'display_key': 'Fourth Component', 'display_value': 'Pumpkin Seeds'}, {'id': 45563, 'key': 'fifth-component', 'value': 'Roast Butternut', 'display_key': 'Fifth Component', 'display_value': 'Roast Butternut'}, {'id': 45564, 'key': 'sixth-component', 'value': 'Feta', 'display_key': 'Sixth Component', 'display_value': 'Feta'}, {'id': 45565, 'key': 'size', 'value': 'Medium', 'display_key': 'Size', 'display_value': 'Medium'}, {'id': 45609, 'key': '_reduced_stock', 'value': '1', 'display_key': '_reduced_stock', 'display_value': '1'}], 'sku': '', 'price': 45, 'parent_name': 'Salads - SOLD OUT'}, {'id': 3162, 'name': 'Salads', 'product_id': 4278, 'variation_id': 4280, 'quantity': 1, 'tax_class': '', 'subtotal': '45.00', 'subtotal_tax': '0.00', 'total': '45.00', 'total_tax': '0.00', 'taxes': [], 'meta_data': [{'id': 45575, 'key': 'bases', 'value': 'Baby Spinach', 'display_key': 'Bases', 'display_value': 'Baby Spinach'}, {'id': 45576, 'key': 'proteins', 'value': 'Steak Strips', 'display_key': 'Proteins', 'display_value': 'Steak Strips'}, {'id': 45577, 'key': 'first-component', 'value': 'Roast Butternut', 'display_key': 'First Component', 'display_value': 'Roast Butternut'}, {'id': 45578, 'key': 'second-component', 'value': 'Feta', 'display_key': 'Second Component', 'display_value': 'Feta'}, {'id': 45579, 'key': 'third-component', 'value': 'Carrots', 'display_key': 'Third Component', 'display_value': 'Carrots'}, {'id': 45580, 'key': 'fourth-component', 'value': 'Pumpkin Seeds', 'display_key': 'Fourth Component', 'display_value': 'Pumpkin Seeds'}, {'id': 45581, 'key': 'fifth-component', 'value': 'Chickpea and Red beans', 'display_key': 'Fifth Component', 'display_value': 'Chickpea and Red beans'}, {'id': 45582, 'key': 'sixth-component', 'value': 'Organic Sprouts', 'display_key': 'Sixth Component', 'display_value': 'Organic Sprouts'}, {'id': 45583, 'key': 'size', 'value': 'Medium', 'display_key': 'Size', 'display_value': 'Medium'}, {'id': 45610, 'key': '_reduced_stock', 'value': '1', 'display_key': '_reduced_stock', 'display_value': '1'}], 'sku': '', 'price': 45, 'parent_name': 'Salads - SOLD OUT'}, {'id': 3163, 'name': 'Salads', 'product_id': 4278, 'variation_id': 4280, 'quantity': 1, 'tax_class': '', 'subtotal': '45.00', 'subtotal_tax': '0.00', 'total': '45.00', 'total_tax': '0.00', 'taxes': [], 'meta_data': [{'id': 45593, 'key': 'bases', 'value': 'Wholewheat protein pasta', 'display_key': 'Bases', 'display_value': 'Wholewheat protein pasta'}, {'id': 45594, 'key': 'proteins', 'value': 'Bacon', 'display_key': 'Proteins', 'display_value': 'Bacon'}, {'id': 45595, 'key': 'first-component', 'value': 'Mini Broccoli', 'display_key': 'First Component', 'display_value': 'Mini Broccoli'}, {'id': 45596, 'key': 'second-component', 'value': 'Baby Cob', 'display_key': 'Second Component', 'display_value': 'Baby Cob'}, {'id': 45597, 'key': 'third-component', 'value': 'Carrots', 'display_key': 'Third Component', 'display_value': 'Carrots'}, {'id': 45598, 'key': 'fourth-component', 'value': 'Pumpkin Seeds', 'display_key': 'Fourth Component', 'display_value': 'Pumpkin Seeds'}, {'id': 45599, 'key': 'fifth-component', 'value': 'Feta', 'display_key': 'Fifth Component', 'display_value': 'Feta'}, {'id': 45600, 'key': 'sixth-component', 'value': 'Roast Butternut', 'display_key': 'Sixth Component', 'display_value': 'Roast Butternut'}, {'id': 45601, 'key': 'size', 'value': 'Medium', 'display_key': 'Size', 'display_value': 'Medium'}, {'id': 45611, 'key': '_reduced_stock', 'value': '1', 'display_key': '_reduced_stock', 'display_value': '1'}], 'sku': '', 'price': 45, 'parent_name': 'Salads - SOLD OUT'}], 'tax_lines': [], 'shipping_lines': [{'id': 3164, 'method_title': 'Distance Rate (1.5 km; 4 mins)', 'method_id': 'distance_rate', 'instance_id': '3', 'total': '3.00', 'total_tax': '0.00', 'taxes': [], 'meta_data': []}], 'fee_lines': [], 'coupon_lines': [], 'refunds': [], 'date_created_gmt': '2021-03-04T18:06:28', 'date_modified_gmt': '2021-03-04T16:09:20', 'date_completed_gmt': '2021-02-20T06:28:43', 'date_paid_gmt': '2021-02-20T06:28:43', 'currency_symbol': 'R', '_links': {'self': [{'href': 'redacted'}], 'collection': [{'href': 'redacted'}], 'customer': [{'href': 'redacted'}]}}]

标签: pythonjsondictionarywhile-loop

解决方案


“API 的输出是列表中的字典”

引用this,也许你在列表中添加一个列表?

在此代码: 之后all_orders.append(requests.get('API location next page').json())all_orders将有内部列表。

也许通过以下方式获得价值:

all_orders[orders_collected - 1][some index]['status']

错误修正:更改appendextend

all_orders.extend(requests.get('API location next page').json())

推荐阅读