[英]PYTHON IndexError: tuple index out of range
非常感謝您對此問題的反饋
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
tag2
只是一個值,就像tag1
一樣,因此您不能引用item [1]。 毫無疑問,你的意思是
print('Second revision: {0}'.format(tag2))
照常使用格式設置時,請記住,在大多數編程語言中,計數從零開始。 因此,由於tag2
僅攜帶一個值,因此以下行:
print('Second revision: {1}'.format(tag2))
確實應該是:
print('Second revision: {0}'.format(tag2))
如果使用python 2.7+,也可以將其保留為空以用於簡單腳本:
print('Second revision: {}'.format(tag2))
或以任何順序為它們提供命名變量:
print('Second revision: {revisiontag}'.format(revisiontag=tag2))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.