1

I am trying to fetch data using API from a website. Thousandeyes API

My code is given below :

import requests
import json
import urllib2, base64
import sys

proxy = urllib2.ProxyHandler({'https': 'http://proxy.wdf.sap.corp:8080'})
opener = urllib2.build_opener(proxy)
urllib2.install_opener(opener)

username='xxx@xxx.com' # API Username
password='xxxxxxxxxxxxxxxxxxx' # API Password Token Code
THOUSANDEYES_API_URL='https://api.thousandeyes.com/v6/usage.json'


request = urllib2.Request(THOUSANDEYES_API_URL)
base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)
result = urllib2.urlopen(request)
data = json.load(result)

#save output in text file

orig_stdout = sys.stdout
f = open ('usage.txt', 'w')
sys.stdout = f

#print data

for i in data['usage']:
        print "Month Start: " + i['monthStart']
        print "Month End: " + i['monthEnd']
        print "Account Group Name:  " + i['accountGroupName']
        print "Test Name: " + i['testName']
        print "\n"

sys.stdout = orig_stdout
f.close()

When I am running this script, I am getting below mention error:

Traceback (most recent call last): File "./te_license_usage.py", line 32, in print "Month Start: " + i['monthStart'] TypeError: string indices must be integers

Please suggest the solution.

Aditi
  • 820
  • 11
  • 27

2 Answers2

0

data['usage'] contains a list which as shown in your error:

 print "Month Start: " + i['monthStart'] TypeError: string indices must be integers

so use data['usage'][0]:

for i in data['usage'][0]:
    print "Month Start: " + i['monthStart']
    print "Month End: " + i['monthEnd']
    print "Account Group Name:  " + i['accountGroupName']
    print "Test Name: " + i['testName']
    print "\n"
Joseph D.
  • 11,804
  • 3
  • 34
  • 67
-1

You do not need to iterate over your data.It is not a list.

Try:

print "Month Start: " + data['usage']["quota"]['monthStart']         #Key "quota"
print "Month Start: " + data['usage']["quota"]['monthEnd']
for j in data['usage']['tests']:                                     #Iterate Key 'tests'
    print "Account Group Name:  " + j['accountGroupName']
    print "Test Name: " + j['testName']

Output:

Month Start: 2016-04-28 00:00:00
Month Start: 2016-05-28 00:00:00
Account Group Name:  Documentation
Test Name: https://app.thousandeyes.com
Account Group Name:  Documentation
Test Name: https://support.thousandeyes.com
Rakesh
  • 81,458
  • 17
  • 76
  • 113
  • Hi Rakesh, Thanks for your kind support. I am still getting same error. – Sunil Kumar Mar 30 '18 at 07:42
  • for i in data['usage']: print "Month Start: " + i["quota"]['monthStart'] print "Month End: " + i["quota"]['monthEnd'] for j in i['tests']: print "Account Group Name: " + j['accountGroupName'] print "Test Name: " + j['testName'] Traceback (most recent call last): File "./te_license_usage.py", line 32, in print "Month Start: " + i["quota"]['monthStart'] TypeError: string indices must be integers – Sunil Kumar Mar 30 '18 at 07:43
  • Updated snippet. – Rakesh Mar 30 '18 at 07:49
  • Great. It is working now. Thanks Rakesh for your great support. – Sunil Kumar Mar 30 '18 at 08:02
  • You are welcome – Rakesh Mar 30 '18 at 08:02