使用字典和字符串的Python字符串插值
给定:
dict = {"path": "/var/blah"}
curr = "1.1"
prev = "1.0"
插入字符串以生成以下内容的最佳/最短方法是什么:
路径:/var/blah 当前:1.1 上一个:1.0
我知道这有效:
str = "path: %(path)s curr: %(curr)s prev: %(prev)s" % {"path": dict["path"],"curr": curr, "prev": prev}
但我希望有一个更短的方法,例如:
str = "path: %(path)s curr: %s prev: %s" % (dict, curr, prev)
如果这看起来像一个过于迂腐的问题,我很抱歉。
Given:
dict = {"path": "/var/blah"}
curr = "1.1"
prev = "1.0"
What's the best/shortest way to interpolate the string to generate the following:
path: /var/blah curr: 1.1 prev: 1.0
I know this works:
str = "path: %(path)s curr: %(curr)s prev: %(prev)s" % {"path": dict["path"],"curr": curr, "prev": prev}
But I was hoping there is a shorter way, such as:
str = "path: %(path)s curr: %s prev: %s" % (dict, curr, prev)
My apologies if this seems like an overly pedantic question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
你可以试试这个:
You can try this:
当然,您可以使用较新的(从 2.6 开始).format 字符串方法:
或者,如果所有元素都在字典中,您可以这样做:
从 str.format() 文档:
And of course you could use the newer (from 2.6) .format string method:
Or, if all elements were in the dictionary, you could do this:
From the str.format() documentation:
为什么不:
顺便说一句,我已经更改了您使用的几个名称,这些名称践踏了内置名称 - 不要这样做,它永远不需要,并且偶尔会浪费您大量时间来追踪它导致的不当行为(其中某些东西使用内置名称,假设它意味着内置名称,但您已使用我们自己的变量名称隐藏了它)。
Why not:
BTW, I've changed a couple names you were using that trample upon builtin names -- don't do that, it's never needed and once in a while will waste a lot of your time tracking down a misbehavior it causes (where something's using the builtin name assuming it means the builtin but you have hidden it with the name of our own variable).
也许:
我的意思是它有效:
我只是不知道你是否认为它更短。
Maybe:
I mean it works:
I just don't know if you consider that shorter.
您还可以(很快)在 Python 3.6 中使用 f-strings,这是可能是格式化字符串的最短方法:
甚至将所有数据放入字典中:
You can also (soon) use f-strings in Python 3.6, which is probably the shortest way to format a string:
And even put all your data inside a dict:
如果将数据放入字典中,您可以执行以下操作:
You can do the following if you place your data inside a dictionary:
2016 年更新:从 Python 3.6 开始,您可以将变量替换为按名称排序字符串:
请注意
f"
前缀。如果您在 Python 3.5 或更早版本中尝试此操作,您将收到SyntaxError
。请参阅 https://docs.python.org/3.6/reference/lexical_analysis.html#f-strings
Update 2016: As of Python 3.6 you can substitute variables into strings by name:
Note the
f"
prefix. If you try this in Python 3.5 or earlier, you'll get aSyntaxError
.See https://docs.python.org/3.6/reference/lexical_analysis.html#f-strings
如果您不想每次都将不变的变量添加到字典中,可以使用以下格式引用变量和字典键:
str = "path {path} curr: {curr} prev: {prev} ".format(curr=curr, prev=prev, **dict)
从逻辑上讲,这可能是不好的形式,但它使事情更加模块化,期望 curr 和 prev 大部分是静态的,并且字典会更新。
If you don't want to add the unchanging variables to your dictionary each time, you can reference both the variables and the dictionary keys using format:
str = "path {path} curr: {curr} prev: {prev}".format(curr=curr, prev=prev, **dict)
It might be bad form logically, but it makes things more modular expecting curr and prev to be mostly static and the dictionary to update.