将github问题导出到csv

时间:2015-05-08 22:13:22

标签: python csv github

我正在尝试将github问题列表从存储库导出到csv,但我一直遇到一些错误。我已尝试在其他问题上进行调查,但他们似乎没有帮助我。我目前在SLES12 vm上使用python2.7.9。

def write_issues(response):
    for issue in response.json():
        labels = issue['labels']
        for label in labels:
            if label['name'] == "Client Requested":
                csvout.writerow([issue['number'],
                issue['title'].encode('utf-8'),
                issue['body'].encode('utf-8'),
                issue['created_at'],
                issue['updated_at']])

收到这些错误

Traceback (most recent call last):
  File "export.py", line 50, in <module>
    write_issues(r)
  File "export.py", line 24, in write_issues
    labels = issue['labels']
TypeError: string indices must be integers

1 个答案:

答案 0 :(得分:1)

response.json()肯定不包含“问题”词典列表。您要么对无效或不正确的端点发出请求,要么点击rate limit。在循环之前检查response.json()的实际值。

适合我:

>>> import requests
>>> 
>>> url = "https://api.github.com/repos/angular/protractor/issues"
>>> response = requests.get(url)
>>> for issue in response.json():
...     labels = issue['labels']
...     for label in labels:
...         print label
... 
{u'url': u'https://api.github.com/repos/angular/protractor/labels/type:%20docs', u'color': u'5319e7', u'name': u'type: docs'}
{u'url': u'https://api.github.com/repos/angular/protractor/labels/type:%20question', u'color': u'f7c6c7', u'name': u'type: question'}
{u'url': u'https://api.github.com/repos/angular/protractor/labels/type:%20feature%20request', u'color': u'009800', u'name': u'type: feature request'}
...