假設我有一個帶有點的字典名稱的字典,如{'person.name': 'Joe'}
。如果我想在str.format
中使用這個,有可能嗎?python:格式字符串中變量的名稱中的點
我的第一反應是
'Name: {person.name}'.format(**{'person.name': 'Joe'})
但如果我的字典中形如
{'person':{'name':Joe}}
The relevant manual docs section無論如何逃脫點並沒有提到這隻會工作。
(旁註:我認爲一般
def func(**kw): print(kw)
func(**{'a.b': 'Joe'})
會導致錯誤,但**
-expanded函數調用似乎即使他們不有效標識符來工作,它確實錯誤了! 。非字符串雖然O_O)
你試過用反斜槓轉義點嗎? –
@RafeKettler:是的。這不起作用,因爲你是爲字符串而不是格式說明符轉義的。 – nfirvine
可能的重複[如何在Python格式字符串中使用點?](http://stackoverflow.com/questions/29035168/how-to-use-a-dot-in-python-format-strings) –