pythonamazon-mwslistings

How can I parse this report tab-delimited response? Amazon MWS


I am trying to make a web app with Amazon MWS. User's can add, list, delete their products in this app and also list their orders.

For listing their products I am trying to use Reports API with "_GET_MERCHANT_LISTINGS_DATA_". But this method is returns with very bad tab-delimited response. And also when I make request with RequestReport method, It sends the listings report to the shop owner.

This is the dummy response example:

b'item-name\titem-description\tlisting-id\tseller-sku\tprice\tquantity\topen-date\timage-url\titem-is-marketplace\tproduct-id-type\tzshop-shipping-fee\titem-note\titem-condition\tzshop-category1\tzshop-browse-path\tzshop-storefront-feature\tasin1\tasin2\tasin3\twill-ship-internationally\texpedited-shipping\tzshop-boldface\tproduct-id\tbid-for-featured-placement\tadd-delete\tpending-quantity\tfulfillment-channel\nPropars deneme urunu 2 CD-ROM CD-ROM\tThis is a development test product\t0119QL9BRT8\tPARS12344321\t0.5\t9\t2016-01-19 05:26:44 PST\t\ty\t4\t\t\t11\t\t\t\tB01ATBY2NA\t\t\t1\t\t\t8680925084020\t\t\t0\tDEFAULT\n'

Is there anybody know another method for listing products in a shop, or what do you suggest for taking better results from "_GET_MERCHANT_LISTINGS_DATA_" report?

Or how can I parse this tab-delimited string?

Thanks.


Solution

  • You really have two options for obtaining bulk data from amazon, tab-delimited and xml. tab-delimited reads in Excel quite nicely actually, and the routines for splitting the values into usable format are pretty straight forward. Unfortunately Amazon doesn't give you the option of XML or Flat File on every report so you have to use a mix of both under most circumstances.

    First off, your title indicates you need to list all listings active and inactive. That is going to be a combination of reports. If you want an 'all inclusive' including problem listings, active listings, hidden listings and cancelled listings you will need three reports:

    All of these are flat file format so you will have a consistent method of reading the data in. In c# you simply read a line, split that line and read each array value. There will be a similar method of performing this in python, which is most likely well documented here on SO. The c# method would look something like this:

    while ((line = file.ReadLine()) != null)
    {
        if (counter == 0)
        {
           string[] tempParts = line.Split(delimiters);
           for (int i = 0; i < tempParts.Length; i++)
           {
               tempParts[i] = tempParts[i].Trim(); //Clean up remaining whitespace.
           }
           //Try and verify headers have not changed.
           if (!isReportHeaderValid(tempParts))
           {
               reportStatus.IsError = true;
               reportStatus.Exception = new Exception("Report Column headers were not validated!!!!");
            return;
           }
           counter++;
           continue;
       }
       counter++;
       string[] parts = line.Split(delimiters);
       for (int i = 0; i < parts.Length; i++)
       {
          parts[i] = parts[i].Trim(); //Clean up remaining whitespace.
       }
       //Do stuff with parts[1], parts[2] etc
    }
    

    This is an example from one of my pieces of code working with the Amazon Inventory report. Basically, I verify that headers are what I would expect them to be (indicating the report format has not changed) and then I split, clean up white space, and work with each element from split.

    Python split method: Python Split

    Alternatively, you could probably take the entire stream and stick it straight into an excel spreadsheet since Excel understands how to delimit on tabs.

    Edit

    Just a note, in my code example i pass 'delimiters' in to the split routine, but I never define it. It is defined as char[] delimiters = new char[] { '\t' };