Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取TypeError:Python中的字符串索引必须是整数_Python_Python 3.x - Fatal编程技术网

获取TypeError:Python中的字符串索引必须是整数

获取TypeError:Python中的字符串索引必须是整数,python,python-3.x,Python,Python 3.x,我正在尝试使用API从网站获取数据 我的代码如下: 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.

我正在尝试使用API从网站获取数据

我的代码如下:

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()
当我运行此脚本时,我发现以下提到的错误:

回溯(最后一次调用):文件“/te\u license\u usage.py”, 第32行,输入 打印“月开始:”+i['monthStart']类型错误:字符串索引必须是整数


请建议解决方案。

您不需要迭代
数据。
不是列表

试试看:

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']
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
输出:

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']
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

data['usage']
包含一个列表,如错误中所示:

 print "Month Start: " + i['monthStart'] TypeError: string indices must be integers
所以使用
数据['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"

嗨,拉凯什,谢谢你的支持。我仍然收到相同的错误。对于数据中的I['usage']:print“Month Start:”+I[“quota”['monthStart']print“Month End:”+I[“quota”['monthEnd']对于I['tests']中的j:+I[“quota”['monthEnd']print“帐户组名称:”+j['accountGroupName']print“测试名称:”+j['testName']回溯(最近一次调用):文件“/te_license_usage.py”,第32行,打印“月开始:”+i[“quota”][“monthStart”]类型错误:字符串索引必须是整数更新的snippet.Great。它现在正在工作。感谢Rakesh对您的大力支持。欢迎您。如果您可以发布一组返回的虚拟数据,这将非常有用。该站点请求登录证书,因为它不工作。正在执行以下错误回溯(最近一次调用last):文件“/te_license_usage.py”,第31行,in for i in data['usage'][0]:KeyError:0