`TypeError:字符串索引必须是整数`

时间:2015-08-31 18:48:14

标签: python python-2.7 github3.py

我试图在存储库中的拉取请求中列出标题和编号。我想将JSON作为dict返回并打印拉取请求的标题和编号。

如果我只是单独打印标题或数字,我会得到预期的输出,但如果将这些值合并打印,我会得到mapfun <- function(val) { res <- as.character(val) res <- replace(x = res, list = which(res == "1"), values = "A") res <- replace(x = res, list = which(res == "2"), values = "B") res <- replace(x = res, list = which(res == "3"), values = "C") res <- replace(x = res, list = which(res == "4"), values = "D") return(matrix(res, ncol = 4)) # ncol(val) } mat1 <- matrix(sample(c(1,2,3,4), 16, replace=T, prob=c(0.25,0.25,0.25,0.25)), nrow=4, ncol=4) mat1 mapfun(mat1)

TypeError: string indices must be integers

1 个答案:

答案 0 :(得分:2)

确实,两位炼金术士说的是真的。给出这个例子:

>>> auth = dict(username='larsks', token='mytoken')
>>> gh = login(**auth)
>>> result = gh.repository(owner='ansible', repository='ansible').pull_request(12165)
>>> data = result.as_dict()

我们可以看到data['title']是一个字符串:

>>> data['title']
'Be systematic about parsing and validating hostnames and addresses (proof of concept)'

如果我们想要PR号码,我们可以要求:

>>> data['number']
12165