'how to escape colon in {xlink:href}'.format_map(属性)?
'how to escape colon in {xlink:href}'.format_map(attributes)?
总结: '{key:spec}'.format_map(dic)
允许格式化 key
访问的 dic
中的值。 spec
表示应该如何格式化。但是,如果我希望分隔冒号成为键的一部分怎么办?我应该如何判断冒号不是分隔符并且下一个字符不是规范?
详细信息: 我使用字符串模板将 XML 属性转换为另一个文本。比如说,我在 attributes
字典中有一个 XML 元素的属性。其中之一具有键 'xlink:href'
(属性的字面名称)。使用.format_map()
方法时,格式字符串应该怎么写?
'{xlink:href}'.format_map(attributes)
不起作用。 Python 抱怨 KeyError: 'xlink'
。 (href
可能会被认为是错误的规范,但异常会停止进一步处理。)
无法在 {xlink:href}
中转义冒号。
You can't specify arbitrary keys in the replacement field:
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>
总结: '{key:spec}'.format_map(dic)
允许格式化 key
访问的 dic
中的值。 spec
表示应该如何格式化。但是,如果我希望分隔冒号成为键的一部分怎么办?我应该如何判断冒号不是分隔符并且下一个字符不是规范?
详细信息: 我使用字符串模板将 XML 属性转换为另一个文本。比如说,我在 attributes
字典中有一个 XML 元素的属性。其中之一具有键 'xlink:href'
(属性的字面名称)。使用.format_map()
方法时,格式字符串应该怎么写?
'{xlink:href}'.format_map(attributes)
不起作用。 Python 抱怨 KeyError: 'xlink'
。 (href
可能会被认为是错误的规范,但异常会停止进一步处理。)
无法在 {xlink:href}
中转义冒号。
You can't specify arbitrary keys in the replacement field:
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>