Example 8.13. Introducing dictionary−based string formatting
-
[原文]P112
>>> params = {"server":"mpilgrim", "database":"master", "uid":"sa", "pwd":"secret"}
>>> "%(pwd)s" % params -------------------------------------------------------------------------------1
'secret'
>>> "%(pwd)s is not a good password for %(uid)s" % params-------------------------------2
'secret is not a good password for sa'
>>> "%(database)s of mind, %(database)s of body" % params------------------------------3
'master of mind, master of body'
-
1
[原文]P112
Instead of a tuple of explicit values, this form of string formatting uses a dictionary, params. And instead of a simple %s marker in the string, the marker contains a name in parentheses. This name is used as a key in the params dictionary and subsitutes the corresponding value, secret, in place of the %(pwd)s marker.
[pope译]
替代tuple(explicit values 明确的值 )的方式,使用字典参数。在字符串中使用带括号的(in parentheses)参数名称 代替字符串中简单的%s标记的 .此参数名称是字典中的键值, 用来替换相应的(corresponding)值,用来替换%(pwd)s 标记。
[net译 来源:http://woodpecker.org.cn/diveintopython/html_processing/dictionary_based_string_formatting.html]
这种字符串格式化形式不用显式的值的 tuple,而是使用一个 dictionary,params。并且标记也不是在字符串中的一个简单 %s,而是包含了一个用括号包围起来的名字。这个名字是 params dictionary 中的一个键字,所以 %(pwd)s 标记被替换成相应的值 secret。
[popexizhi:
1. instead of a tuple of explicit values
[pope译]替代tuple(explicit values 明确的值 )的方式
[net 译]这种字符串格式化形式不用显式的值的 tuple
[popexizhi] net 把explicit 翻译了,但是pope还是认为这个中文说出来还古怪。explicit 有明确的,还有露骨的解释,自己多少都感觉,相对后面的dictionary这里的explicit 说的更多的是原始的真实,不加掩饰的真实下的简陋之含义,但是没有想到太好的中文短语言简意赅的表达,还是干脆不翻译来的准确表达原句的含义。“显式”好吧!还是感觉太单薄无力了。
2. This name is used as a key in the params dictionary and subsitutes the corresponding value, secret, in place of the %(pwd)s marker
[pope译]此参数名称是字典中的键值, 用来替换相应的(corresponding)值,用来替换%(pwd)s 标记。
[net 译]这个名字是 params dictionary 中的一个键字,所以 %(pwd)s 标记被替换成相应的值 secret
[popexizhi]secret 是被替换后的真实密码,这里pope翻译时省略了,看起来正句话有点儿不通顺,改进一下
[pope改进译] 此参数名称是字典中的键值, 用来替换相应的(corresponding)值secret,用来替换%(pwd)s 标记
]
2
[原文]P112
Dictionary−based string formatting works with any number of named keys. Each key must exist in the given dictionary, or the formatting will fail with a KeyError.
[pope改进译]基于字典的字符串格式化可以使用任意长的主键名称。此主键必须在此dictionary中定义,否则会引发格式化的失败,抛出KeyError 的异常。
-------------------------------------------------------------------
[popexizhi]别的都明白,但是这句不太明白在说什么!ok,下文翻译完就了解了,一个没有定义主键名称的keyerror 异常。
[pope译]基于字典的字符串格式化可以使用每一个主键名。此主键必须在字典中存在,否则由于KeyError格式化失败.
[net 译 来源:http://woodpecker.org.cn/diveintopython/html_processing/dictionary_based_string_formatting.html]
基于 dictionary 的字符串格式化可用于任意数量的有名的键字。每个键字必须在一个给定的 dictionary 中存在,否则这个格式化操作将失败并引发一个 KeyError 的异常
[popexizhi:
with any number of named keys
[pope译]可以使用每一个主键名
[net 译]可用于任意数量的有名的键字
[popexizhi] any 应该是说number的吧?!有名字的主键,这个名字可以包含任意数量的字符串。这么表达成中文更合适呢?任意长的主键名称
]
3
[原文]P112
You can even specify the same key twice; each occurrence will be replaced with the same value.
[pope译]
你可以明确(specify)使用同一个主键两次;每次替换(each occurrence)使用相同的值.
[net 译 来源:http://woodpecker.org.cn/diveintopython/html_processing/dictionary_based_string_formatting.html]
您甚至可以两次指定同一键字,每个键字出现之处将被同一个值所替换。
[原文]P112
So why would you use dictionary−based string formatting? Well, it does seem like overkill to set up a dictionary of keys and values simply to do string formatting in the next line; it's really most useful when you happen to have a dictionary of meaningful keys and values already. Like locals.
[pope译]
那么你为什么使用基于字典格式的字符串呢?它看起来就像过度设计的字典主键和值简单的在下一行使用字符串格式化([?]这句很纠结不是吗?);当你使用字典中有意义的主键名称和值后就可以看出这个多么有用了,就像locals
[net 译 来源:http://woodpecker.org.cn/diveintopython/html_processing/dictionary_based_string_formatting.html]
那么为什么您偏要使用基于 dictionary 的字符串格式化呢?的确,仅为了进行字符串格式化,就事先创建一个有键字和值的 dictionary 看上去的确有些小题大作。它的真正最大用处是当您碰巧已经有了像 locals 一样的有意义的键字和值的 dictionary 的时候。
[popexizhi:
Well, it does seem like overkill to set up a dictionary of keys and values simply to do string formatting in the next line;
[pope译]它看起来就像过度设计的字典主键和值简单的在下一行使用字符串格式化
[net译]仅为了进行字符串格式化,就事先创建一个有键字和值的 dictionary 看上去的确有些小题大作。
[popexizhi] overkill to .... simply to net翻译的很漂亮不是吗?!pope的原文如果修改为 "它看起来就像过度设计的字典主键和值仅仅是简单的在下一行使用字符串格式化" 应该会好一点儿理解了:)
]
没有评论:
发表评论