PYTHON IndexError:元组索引超出范围
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24167373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PYTHON IndexError: tuple index out of range
提问by Super_Py_Me
Would really appreciate feedback on this issue
非常感谢对此问题的反馈
import subprocess
def main():
'''
Here's where the whole thing starts.
'''
#Edit this constant to change the file name in the git log command.
FILE_NAME = 'file1.xml'
#Do the git describe command to get the tag names.
gitDescribe = 'git describe --tags `git rev-list --tags --max-count=2`'
print ('Invoking: {0}'.format(gitDescribe))
p1 = subprocess.Popen(gitDescribe, shell=True, stdout=subprocess.PIPE)
output = p1.stdout.read()
#Get the first 2 tags from the output.
parsedOutput = output.split('\n')
tag1 = parsedOutput[0]
tag2 = parsedOutput[1]
print('First revision: {0}'.format(tag1))
print('Second revision: {1}'.format(tag2))
#Do the git log command for the revision comparison.
gitLog = 'git log {0}..{1} --pretty=format:"%an %h %ad %d %s" --date=short --topo-order --no-merges {2}'.format(tag1, tag2, FILE_NAME)
print('Invoking: {0}'.format(gitLog))
p2 = subprocess.Popen(gitLog, shell=True, stdout=subprocess.PIPE)
output = p2.stdout.read()
print(output)
if __name__ == "__main__":
main()
...
...
bash-3.2$ python pygit5.py
Invoking: git describe --tags `git rev-list --tags --max-count=2`
First revision: 14.5.5.1
Traceback (most recent call last):
File "pygit5.py", line 31, in <module>
main()
File "pygit5.py", line 22, in main
print('Second revision: {1}'.format(tag2))
IndexError: tuple index out of range
采纳答案by Daniel Roseman
tag2
is only a single value, like tag1
, so you can't reference item[1]. No doubt you mean
tag2
只是一个值,比如tag1
,所以你不能引用 item[1]。毫无疑问你的意思是
print('Second revision: {0}'.format(tag2))
回答by Simon Fraser
When using formatting as you have, remember that in most programming languages, counting starts from zero. So since tag2
only carries one value, the following line:
在使用格式化时,请记住,在大多数编程语言中,计数从零开始。因此,由于tag2
仅携带一个值,因此以下行:
print('Second revision: {1}'.format(tag2))
Should really be:
真的应该是:
print('Second revision: {0}'.format(tag2))
You can also leave it empty for simple scripts if using python 2.7+:
如果使用 python 2.7+,您也可以将简单脚本留空:
print('Second revision: {}'.format(tag2))
Or provide them in any order with named variables:
或者以任何顺序为它们提供命名变量:
print('Second revision: {revisiontag}'.format(revisiontag=tag2))