2017-04-18 2 views
0

모든 파이썬 참조 JSON 배열

I .... 스크립트 내가 그러나 내가 그들을 찾을 수 없습니다, 샤 해시를 열려있는 모든 풀 요청을 찾아서 비교 구축하고있다

for repo in g.get_user().get_repos(): 
print (repo.full_name) 
json_pulls = requests.get('https://api.github.com/repos/' + repo.full_name + '/pulls?state=open+updated=<' + str(cutoff_date.date())+ '&sort=created&order=asc') 
if (json_pulls.ok): 
    for item in json_pulls.json(): 
     for c in item.items(): 
      #print(c["0"]["title"]) 
      #print (json.dumps(state)) 
      print(c) 
기존의 repos를 통해

코드주기와는 풀 요청을 나열하고 나는 출력을 얻을 :

output

을하지만, 난 내 인생에 대한 폐기물 수집하는 방법을 알아낼 수 없습니다

  • print(c['0']['title'])이 -a 관 오류가
  • 은 내가 무엇을 찾고 있어요 것은 인 오류를 정의되지 않은 -

    1. print(c['title']) :

      내가 참조를 사용하여 시도 ... 개별 필드에서 t 각 요청에 대한 간단한 목록 ....

      title 
      id 
      state 
      base/sha 
      head/sha 
      

      캔 누군가가 내 파이썬 스크립트에서 json 항목을 참조 할 때 내가 미치고있는 것을 지적하고있다.

      으로 전체 코드는 ... 물론 당신의 도움이 함께 :

      # py -m pip install <module> to install the imported modules below. 
      # 
      # 
      # Import stuff 
      from github import Github 
      from datetime import datetime, timedelta 
      import requests 
      import json 
      import simplejson 
      # 
      # 
      #declare stuff 
      # set the past days to search in the range 
      PAST = 5 
      
      # get the cut off date for the repos 10 days ago 
      cutoff_date = datetime.now() - timedelta(days=PAST) 
      #print (cutoff_date.date()) 
      
      # Repo oauth key for my repo 
      OAUTH_KEY = "(get from github personal keys)" 
      
      # set base URL for API query 
      BASE_URL = 'https://api.github.com/repos/' 
      
      # 
      # 
      # BEGIN STUFF 
      
      # First create a Github instance: 
      g = Github(login_or_token=OAUTH_KEY, per_page=100) 
      
      # get all repositories for my account that are open and updated in the last 
      no. of days.... 
      for repo in g.get_user().get_repos(): 
      print (repo.full_name) 
      json_pulls = requests.get('https://api.github.com/repos/' + repo.full_name 
      + '/pulls?state=open+updated=<' + str(cutoff_date.date())+ 
      '&sort=created&order=asc') 
      if (json_pulls.ok): 
          for item in json_pulls.json(): 
           print(item['title'], item['id'], item['state'], item['base']['sha'], 
          item['head']['sha']) 
      

      되찾기 사이트는 두 가지의 repos와 대결하는 1 명 또는 2 개의 풀 요청, 간단한 사이트입니다.

      스크립트가 끝나면 모든 repos를 반복하고, x 일보다 오래되어서 열린 요청을 찾고, 분기에 대한 sha (그리고 master 브랜치는 sha, 추가하려면 ...., 가지를 마스터하지 않는 가지를 제거하여 예전의 코드를 제거하고 깔끔한 REPOS을 유지하기 위해 요청을 당겨) .....

    +0

    for 루프 앞에 논리를 게시 할 수도 있습니까 (https://stackoverflow.com/help/mcve 참조). 그러면 잘라 내기를 복사하여 테스트 할 수 있습니까? –

    +0

    스크린 샷을 덤프하십시오. 우리에게 예제 입력 행을 테스트 해보십시오. – tdelaney

    +0

    'item [ "title"]'등 ... – tdelaney

    답변

    2

    json_pulls.json()는 당신이 단지 수 있도록 사전의 목록을 반환 item.items()을 반복 할 필요가

    for item in json_pulls.json(): 
        print (item['title'], item['id'], item['state'], item['base']['sha'], item['head']['sha']) 
    

    없습니다.

    +0

    @tdelaney github 문서를 확인한 후 JSON 객체를 반환한다.이 객체는 Python dict로 변환되어야한다. – Barmar

    +0

    죄송합니다. – Barmar

    +0

    예 .... 그 것이었다. .. 나를 위해 멋지게 일했다. .. 도와 줘서 고마워! –

    0
    for key, value in item.items(): 
        if (key == 'title'): 
         print(value) 
         #Do stuff with the title here 
    

    를 건너 뜁니다 : JSON은 딕셔너리로 ​​반환해야합니다, 파이썬에서 for 명령은 dictionary.items()에서 호출 될 때 key->val 쌍을 기본 검색합니다.

    +1

    'item '에'title'을 쓰는 것은 비싼 방법이다. – tdelaney