根据Python str.format()文档,什么是有效键 [英] what are valid keys according to Python str.format() documentation

查看:109
本文介绍了根据Python str.format()文档,什么是有效键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python的新手,只需阅读Python文档

I'm new to Python and just read the following part from Python doc 6.1.3. Format String Syntax:

Because arg_name is not quote-delimited, it is not possible to specify arbitrary dictionary keys (e.g., the strings '10' or ':-]') within a format string.

这是什么意思?有人可以给我例子吗?这是否意味着格式字符串中的所有数字都是位置参数?那么"10"是第十个论点吗?对于':-]',是因为它具有']'?

What does this mean? Can someone give me any examples? Does this mean that all numbers in a format string are positional arguments? So '10' is the 10th argument? For ':-]', is this because it has ']'?

我也看到了这个另一个问题来自帖子:

dictionary = {'key1': 'val1', '1': 'val2'}

string1 = 'Interpolating {0[key1]}'.format(dictionary)
print string1

为什么不是{0 ["key1"]}?键"key1"是字符串,但是如果使用"key1",则为错误.普通的dict索引应该是dictionary ['key1'],而不是 字典[key1]

Why it is not {0["key1"]}? The key 'key1' is a string, but here if using "key1", it is an error. A normal dict index should be dictionary['key1'], not dictionary[key1]

就像@BrenBarn在该帖子中回答的那样,如果使用数字1作为字典中的键,它可以工作.

And just as @BrenBarn replied in that post, if using number 1 as the key in dictionary, it can work.

dictionary = {'key1': 'val1', 1: 'val2'}

string2 = 'Interpolating {0[1]}'.format(dictionary)
print string2

所以我对格式字符串中有效的index_string感到困惑.

So I'm confused about what the valid index_string are in a format string.

谢谢

推荐答案

我认为我在阅读并思考了更多内容后才知道答案.感谢@decimus phostle在 PEP .几乎有一条相同的说法终于回答了我的问题:

I think I know the answer after reading and thinking more. Thanks to @decimus phostle in that post which mentioned the PEP. There is a nearly same statement that finally answered my question:

Because **keys** are not quote-delimited, it is not possible to
specify arbitrary dictionary keys (e.g., the strings "10" or
":-]") from within a format string.

区别在于它在PEP中显示的是,而不是 arg_name .因此,不是用引号引起来的"表示引号是键中的普通字符.例如

The difference is that it says keys not arg_name in the PEP. So "not quote-delimited" means quotes are ordinary characters in a key. For example,

{0["1"]}

在此替换字段中,键是三个字符:双引号,1和双引号,而不是一个字符1.因此,显然在字典中没有这样的三字符键.

In this replacement field, the key is three characters: double quote, 1, and double quote, not one character 1. So obviously there is no such three-character key in the dictionary.

根据PEP,查找密钥的规则很简单:如果以数字开头,则为数字;如果以数字开头,则为数字.否则为字符串.这意味着,如果您的字典具有字符串键,但由数字字符组成,例如"10",则无法在替换字段中指定键.因为如果在替换字段中使用10,它将被认为是10;如果使用"10",则它被认为是四个字符的字符串,而不是两个字符"1"和"0"的字符串.

Also accoring to the PEP, it has simple rule to find key: if it starts with a digit, then it is a number; otherwise it is a string. This means that if your dictionary has a key of string, but consist of digit character, for example, '10', there is no way you can specify the key in replacement field. Because if you use 10 in replacment field, it is considered number 10; If you use '10', it is considered string of four characters, not a string of two character '1' and '0'.

以':-]'作为键,为什么不可能呢?因为引号不是分隔符,所以

For ':-]' as key, why it is not possible? Because quotes are not delimiters,

{0[':-]']}

引号不会引起内部]的引号(文字).因此它成为[的匹配项],会过早终止索引.

the quotes won't make the inner ] quoted (literal). So it becomes the matched ] of [, which terminates the index prematurely.

以下是替换字段中要比较的有效密钥:

Here is a valid key in a replacement field to compare:

dd = {"'10'":'a'}    
print("{0['10']}".format(dd))

要在替换字段中使用类似"10"的键,您需要确保字典中有一个名为'10'"的键.

To use something like '10' as key in the replacement field, you need to make sure the dictionary has a key named "'10'".

这篇关于根据Python str.format()文档,什么是有效键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆