python: dots in the name of variable in a format string

后端 未结 3 1141
北荒
北荒 2021-01-11 16:41

Say I\'ve got a dictionary with dots in the name of fields, like {\'person.name\': \'Joe\'}. If I wanted to use this in str.format, is it possible

3条回答
  •  被撕碎了的回忆
    2021-01-11 17:13

    One way to work around this is to use the old % formatting (which has not been deprecated yet):

    >>> print 'Name: %(person.name)s' % {'person.name': 'Joe'}
    Name: Joe
    

提交回复
热议问题