Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.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 str.format中转义点_Python_String Formatting - Fatal编程技术网

如何在python str.format中转义点

如何在python str.format中转义点,python,string-formatting,Python,String Formatting,我希望能够访问字典中带有点str.format()的键。我该怎么做 例如,不带点的键的格式: >>> "{hello}".format(**{ 'hello' : '2' }) '2' 但当钥匙上有一个点时,它不会: >>> "{hello.world}".format(**{ 'hello.world' : '2' }) Traceback (most recent call last): File "<stdin>", line 1, i

我希望能够访问字典中带有点
str.format()
的键。我该怎么做

例如,不带点的键的格式:

>>> "{hello}".format(**{ 'hello' : '2' })
'2'
但当钥匙上有一个点时,它不会:

>>> "{hello.world}".format(**{ 'hello.world' : '2' })
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'hello'
>>“{hello.world}”。格式(**{'hello.world':'2'})
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
KeyError:“你好”
您不能。仅支持整数或有效的Python标识符作为键。从文件中:

其中,
标识符
为:

标识符(也称为名称)由以下词汇定义描述:

identifier ::=  (letter|"_") (letter | digit | "_")*
不允许使用点(或分号)

您可以将字典用作第二级对象:


在这里,我们将字典分配给名称
v
,然后使用一个键名对其进行索引。这些可以是任何字符串,而不仅仅是标识符。

您可以使用-
替换
方法接受字典

变量名中不能有句点或分号。您的变量名没有分号。它有一个周期。可能是
identifier ::=  (letter|"_") (letter | digit | "_")*
"{v[hello.world]}".format(v={ 'hello.world' : '2' })