Piyush Patil
Piyush Patil

Reputation: 14523

How skip to another loop in python if no data returned by the API?

I have a python code that loops through multiple location and pulls data from a third part API. Below is the code sublocation_idsare location id coming from a directory.

As you can see from the code the data gets converted to a data frame and then saved to a Excel file. The current issue I am facing is if the API does not returns data for publication_timestamp for certain location the loop stops and does not proceeds and I get error as shown below the code.

How do I avoid this and skip to another loop if no data is returned by the API?

for sub in sublocation_ids:
    city_num_int = sub['id']
    city_num_str = str(city_num_int)
    city_name = sub['name']
    filter_text_new = filter_text.format(city_num_str)
    data = json.dumps({"filters": [filter_text_new], "sort_by":"created_at", "size":2})
    r = requests.post(url = api_endpoint, data = data).json()
    articles_list = r["articles"] 
    articles_list_normalized = json_normalize(articles_list)
    df = articles_list_normalized
    df['publication_timestamp'] = pd.to_datetime(df['publication_timestamp'])
    df['publication_timestamp'] = df['publication_timestamp'].apply(lambda x: x.now().strftime('%Y-%m-%d'))
    df.to_excel(writer, sheet_name = city_name)
writer.save()   

Key Error: publication_timestamp

Upvotes: 2

Views: 544

Answers (1)

r.ook
r.ook

Reputation: 13848

Change this bit of code:

df = articles_list_normalized
if 'publication_timestamp' in df.columns:
    df['publication_timestamp'] = pd.to_datetime(df['publication_timestamp'])
    df['publication_timestamp'] = df['publication_timestamp'].apply(lambda x: x.now().strftime('%Y-%m-%d'))
    df.to_excel(writer, sheet_name = city_name)
else:
    continue

If the API literally returns no data i.e. {} then you might even do the check before normalizing it:

if articles_list:
    df = json_normalize(articles_list)
    # ... rest of code ...
else:
    continue

Upvotes: 3

Related Questions