0

The following code is giving me the error:

Traceback (most recent call last):   File "AMZGetPendingOrders.py", line 66, in <module>
    item_list.append(item['SellerSKU']) TypeError: string indices must be integers

The code:

from mws import mws
import time
import json
import xmltodict

access_key = 'xx' #replace with your access key
seller_id = 'yy' #replace with your seller id
secret_key = 'zz' #replace with your secret key
marketplace_usa = '00'

orders_api = mws.Orders(access_key, secret_key, seller_id)
orders = orders_api.list_orders(marketplaceids=[marketplace_usa], orderstatus=('Pending'), fulfillment_channels=('MFN'), created_after='2018-07-01')

#save as XML file
filename = 'c:order.xml'
with open(filename, 'w') as f:
    f.write(orders.original)

#ConvertXML to JSON     
dictString = json.dumps(xmltodict.parse(orders.original))

#Write new JSON to file
with open("output.json", 'w') as f:
  f.write(dictString) 

#Read JSON and parse our order number  
with open('output.json', 'r') as jsonfile:
    data = json.load(jsonfile)

#initialize blank dictionary
id_list = []

for order in data['ListOrdersResponse']['ListOrdersResult']['Orders']['Order']:
    id_list.append(order['AmazonOrderId'])

#This "gets" the orderitem info - this code actually is similar to the initial Amazon "get" though it has fewer switches
orders_api = mws.Orders(access_key, secret_key, seller_id)

#opens and empties the orderitem.xml file
open('c:orderitem.xml', 'w').close()


#iterated through the list of AmazonOrderIds and writes the item information to orderitem.xml
for x in id_list:
    orders = orders_api.list_order_items(amazon_order_id = x)
    filename = 'c:orderitem.xml'
    with open(filename, 'a') as f:
        f.write(orders.original)

#ConvertXML to JSON     
amz_items_pending = json.dumps(xmltodict.parse(orders.original))

#Write new JSON to file
with open("pending.json", 'w') as f:
  f.write(amz_items_pending) 

#read JSON and parse item_no and qty
with open('pending.json', 'r') as jsonfile1:
    data1 = json.load(jsonfile1)


#initialize blank dictionary
item_list = []

for item in data1['ListOrderItemsResponse']['ListOrderItemsResult']['OrderItems']['OrderItem']:
    item_list.append(item['SellerSKU']) 


#print(item)
#print(id_list)

#print(data1)
#print(item_list)

time.sleep(10)

I don't understand why Python thinks this is a list and not a dictionary. When I print id_list it looks like a dictionary (curly braces, single quotes, colons, etc)

print(data1) shows my dictionary

{
   'ListOrderItemsResponse':{
      '@xmlns':'https://mws.amazonservices.com/Orders/201 3-09-01',
      'ListOrderItemsResult':{
         'OrderItems':{
            'OrderItem':{
               'QuantityOrdered ':'1',
               'Title':'Delta Rothko Rolling Bicycle Stand',
               'ConditionId':'New',
               'Is Gift':'false',
               'ASIN':'B00XXXXTIK',
               'SellerSKU':'9934638',
               'OrderItemId':'49 624373726506',
               'ProductInfo':{
                  'NumberOfItems':'1'
               },
               'QuantityShipped':'0',
               'C onditionSubtypeId':'New'
            }
         },
         'AmazonOrderId':'112-9XXXXXX-XXXXXXX'
      },
      'ResponseM etadata':{
         'RequestId':'8XXXXX8-0866-44a4-96f5-XXXXXXXXXXXX'
      }
   }
}

Any ideas?

Seanny123
  • 8,776
  • 13
  • 68
  • 124
RzeroBzero
  • 11
  • 1
  • looks like a duplicate of: https://stackoverflow.com/questions/6077675/why-am-i-seeing-typeerror-string-indices-must-be-integers – gregory Jul 28 '18 at 22:57

2 Answers2

0

because you are iterating over each key value in dict:

{'QuantityOrdered ': '1', 'Title': 'Delta Rothko Rolling Bicycle Stand', 'ConditionId': 'New', 'Is Gift': 'false', 'ASIN': 'B00XXXXTIK', 'SellerSKU': '9934638', 'OrderItemId': '49 624373726506', 'ProductInfo': {'NumberOfItems': '1'}, 'QuantityShipped': '0', 'C onditionSubtypeId': 'New'}

so first value in item will be 'QuantityOrdered ' and you are trying to access this string as if it is dictionary

you can just do:

id_list.append(data1['ListOrderItemsResponse']['ListOrderItemsResult']['OrderItems']['OrderItem']['SellerSKU']))

and avoid for loop in dictionary

dilkash
  • 562
  • 3
  • 15
  • That worked! It gave me the SellerSKU for each order. Now if I can ask a followup - do you know how I would be able to pull out SellerSKU and QuantityOrdered separated by a comma? – RzeroBzero Jul 28 '18 at 21:44
  • similarly data1['ListOrderItemsResponse']['ListOrderItemsResult']['OrderItems']['OrderItem']['QuantityOrdered '], mind you there is space at the end of key "QuantityOrdered " , don't forget to include space while accessing , do you mind marking the answer correct if it worked for you :) – dilkash Jul 29 '18 at 07:13
0

I guess you are trying to iterate OrderItems and finding their SellerSKU values.

for item in data1['ListOrderItemsResponse']['ListOrderItemsResult']['OrderItems']:
  item_list.append(item['SellerSKU'])
Ravin Gupta
  • 783
  • 7
  • 13