Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.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 - Fatal编程技术网

Python 字符串格式中的元组索引超出范围

Python 字符串格式中的元组索引超出范围,python,Python,我有一个元组: ('ORF eins', '20:15', '21:05', 'soko-donau.html', 'Soko Donau', 'Schöne neue Welt') 它有六个元素(索引0-5) 如果使用字符串格式打印,如下所示: print("""Entry {} Title: {} Station: {} Start Time: {} End Time: {}""".format(programID, details[4], details[0], details[1]),

我有一个元组:

('ORF eins', '20:15', '21:05', 'soko-donau.html', 'Soko Donau', 'Schöne neue Welt')
它有六个元素(索引0-5)

如果使用字符串格式打印,如下所示:

print("""Entry {}
Title: {}
Station: {}
Start Time: {}
End Time: {}""".format(programID, details[4], details[0], details[1]), details[2])

我得到了一个“索引器:元组索引超出范围”,尽管我只在
4
之前使用索引,元组中有6个元素。

details[1]
之后有一个右括号,这会弄乱代码。

details[1]之后有一个右括号
这会弄乱你的代码。

看起来你的括号放错地方了:

print("""Entry {}
Title: {}
Station: {}
Start Time: {}
End Time: {}""".format(programID, details[4], details[0], details[1]), details[2])
#                                                                   ^
因此,当format语句预期为5时,它得到4个参数(因为有5个“替换槽
{}
”),因此当它尝试获取第5个参数时,它有一个
索引器

使用
“{}.format()
也会得到同样的结果,例如:

>>> "{}".format()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: tuple index out of range
>>“{}”.format()
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
索引器错误:元组索引超出范围

看起来括号放错地方了:

print("""Entry {}
Title: {}
Station: {}
Start Time: {}
End Time: {}""".format(programID, details[4], details[0], details[1]), details[2])
#                                                                   ^
因此,当format语句预期为5时,它得到4个参数(因为有5个“替换槽
{}
”),因此当它尝试获取第5个参数时,它有一个
索引器

使用
“{}.format()
也会得到同样的结果,例如:

>>> "{}".format()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: tuple index out of range
>>“{}”.format()
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
索引器错误:元组索引超出范围

@Abstracted创建多行字符串没有更具可读性的方法了。如果为docstrings保留
很重要,你会希望对它说些什么,除非它在那里,而我错过了它。@Abstracted,没有更可读的方法来创建多行字符串。如果为docstrings保留
很重要,你会希望对它说些什么,除非它在那里,我错过了它。在我发布它几分钟后看到它,现在效果非常好。当然:D无论如何,谢谢!我贴了几分钟后就看到了,现在效果很好当然了:谢谢!