python文档有关于formatting strings语法的信息,但是我似乎无法找到有关如何读取定义替换字段语法的表的信息。
replacement_field ::= "{" [field_name] ["!" conversion] [":" format_spec] "}"
field_name ::= arg_name ("." attribute_name | "[" element_index "]")*
arg_name ::= [identifier | integer]
attribute_name ::= identifier
element_index ::= integer | index_string
index_string ::= <any source character except "]"> +
conversion ::= "r" | "s" | "a"
format_spec ::= <described in the next section>
在format specification section也有类似的表。
我理解表格的一部分,比如::=
分隔了定义和definien,引号内的字符是文字,而|
的意思是“或”,但桌子的其余部分逃脱了我。
这种格式就是所谓的Backus-Naur形式。 More information found on BNF here.基本上,BNF是一组推导规则。
定义符号:
一点点剖析这个例子来帮助你入门:
replacement_field ::= "{" [field_name] ["!" conversion] [":" format_spec] "}"
field_name ::= arg_name ("." attribute_name | "[" element_index "]")*
replacement_field
由可选的field_name
,可选的conversion
和可选的format_spec
组成。括号([和])表示可选参数。
如果你确实将field_name
传递给replacement_field
,它包含一个arg_name
函数,你可以通过attribute_name
或element_index
。注意element_index
是强制性的,因为括号是引号,因此转出BNF表格可选。