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

Question:

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-nametitem-descriptiontlisting-idtseller-skutpricetquantitytopen-datetimage-urltitem-is-marketplacetproduct-id-typetzshop-shipping-feetitem-notetitem-conditiontzshop-category1tzshop-browse-pathtzshop-storefront-featuretasin1tasin2tasin3twill-ship-internationallytexpedited-shippingtzshop-boldfacetproduct-idtbid-for-featured-placementtadd-deletetpending-quantitytfulfillment-channelnPropars deneme urunu 2 CD-ROM CD-ROMtThis is a development test productt0119QL9BRT8tPARS12344321t0.5t9t2016-01-19 05:26:44 PSTttyt4ttt11ttttB01ATBY2NAttt1ttt8680925084020ttt0tDEFAULTn'

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.

Asked By: Yağız Nalçakan

||

Answers:

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:

  • _GET_MERCHANT_LISTINGS_DATA_
  • _GET_MERCHANT_CANCELLED_LISTINGS_DATA_
  • _GET_MERCHANT_LISTINGS_DEFECT_DATA_

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' };

Answered By: Nate M.
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.