-2

I'm trying to list the title and number on a pull request in a repository. I would like to return the JSON as a dict and print the title and number of the pull request.

If I just print the title or number alone, I get the expected output, but if combine the values to print, I get TypeError: string indices must be integers.

#!/usr/bin/env python
import github3
from github3 import login, GitHub
import requests
import json
import sys

auth = dict(username="xxxxxxxxx",token="xxxxxxxxx")
gh = login(**auth)

result = gh.repository(owner="xxx", repository="xxxx").pull_request(x)
data  = result.as_dict()
print data['title']['number']
DBS
  • 1,107
  • 1
  • 12
  • 24
  • 1
    I am not familiar with this API but I suggest that `data['title']` is a string, not a dict as you seem to think. Therefore you are doing something like `'hello'['number']` which I hope you agree makes no sense. – Two-Bit Alchemist Aug 31 '15 at 18:52

1 Answers1

2

Indeed, what Two-Bit Alchemist said is true. Given this example:

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

We can see that data['title'] is a string:

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

If we want the PR number, we can ask for:

>>> data['number']
12165
larsks
  • 277,717
  • 41
  • 399
  • 399
  • 1
    Amazing what you can learn reading error messages. :) – Two-Bit Alchemist Aug 31 '15 at 19:09
  • Iarsks and Two-Bit Alchemist....Thanks for the explanations and example. After looking over the logic it makes sense. I was trying to combine one line into two...`print data['title']['number']`. When I break it up into two print statements....`print data['title']` and then another print statement for `print data['number']`, output is as expcected – DBS Aug 31 '15 at 20:30