Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/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
Python:字符串索引必须是整数_Python_Python 2.7 - Fatal编程技术网

Python:字符串索引必须是整数

Python:字符串索引必须是整数,python,python-2.7,Python,Python 2.7,我正在编写一些Python代码来自动化github合并请求 我在下面找到了以下代码。当我运行这个程序时,我得到了TypeError:字符串索引必须是整数 我发现这里有几个线程引用了这个错误,但我不确定如何在代码中实现修复 #!/usr/bin/env python import json import requests import datetime OAUTH_KEY = "xxxxxxxxxxxx" repos = ['my_app'] # Add all repo's you want

我正在编写一些Python代码来自动化github合并请求

我在下面找到了以下代码。当我运行这个程序时,我得到了TypeError:字符串索引必须是整数

我发现这里有几个线程引用了这个错误,但我不确定如何在代码中实现修复

#!/usr/bin/env python
import json
import requests
import datetime


OAUTH_KEY = "xxxxxxxxxxxx"
repos = ['my_app'] # Add all repo's you want to automerged here
ignore_branches = ['master', 'release', 'staging', 'development'] # Add 'master' here if you don't want to automerge into master


# Print merge/no-merge message to logfile
def print_message(merging):
  if merging == True:
   message = "Merging: "
 else:
  message = "Not merging: "
 print message + str(pr_id) + " - " + user + " wants to merge " + head_ref + " into " + base_ref


# Merge the actual pull request
def merge_pr():
  r = requests.put("https://api.github.com/repos/:owner/%s/pulls/%d/merge"%(repo,pr_id,),
   data=json.dumps({"commit_message": "Auto_Merge"}),
   auth=('token', OAUTH_KEY))
  if "merged" in r.json() and r.json()["merged"]==True:
   print "Merged: " + r.json()['sha']
  else:
   print "Failed: " + r.json()['message']


# Main

print datetime.datetime.now()

for repo in repos:
  r = requests.get('https://api.github.com/repos/:owner/%s/pulls' % repo, auth=('token', OAUTH_KEY))
  data = r.json()

for i in data:
  head_ref=i["head"]["ref"]
  base_ref=i["base"]["ref"]
  user=i["user"]["login"]
  pr_id = i["number"]
  if base_ref in ignore_branches:
   print_message(False)
  else:
   print_message(True)
   merge_pr()

哪一行代码出现问题? 如果是这一行:

”其他:
message=“不合并:”

打印消息+str(pr_id)+“-”+user+”要将“+head_ref+”合并到“+base_ref”
找到的线程对此有何评论?你能通过缩短代码来复制吗?完整的错误消息是什么(包括堆栈跟踪和行号,缩短到最小可复制示例后)。请将这些细节编辑到问题中,而不是评论。(如果您想让我在完成后看一看,请随意评论您已经这样做了。)您可以打印r.json()的结果“data”吗?我的猜测是,您试图像访问列表一样访问dict。错误发生在哪一行?第41行,在head_ref=i[“head”][“ref”]中,您的错误很可能发生在数据块中的
中,对于i,要么
i
是字符串,要么
i[“head”]
i[“base”]
中的一个值返回字符串。如果您发布了
数据的内容
我没有正确地进行身份验证,因此返回了错误的数据,这会有所帮助。我现在已经解决了我的问题。目前代码片段应该只用于HTML/CSS/JavaScript。使用Python的普通代码块。