本文共 3389 字,大约阅读时间需要 11 分钟。
This function was introduced in Python 3.2. It’s syntax is:
此功能在Python 3.2中引入。 它的语法是:
str.format_map(mapping)
Let’s look at some examples of format_map() function.
我们来看一下format_map()函数的一些示例。
s = 'My name is {name} and I am a {job_title}'my_dict = {'name': 'Pankaj', 'job_title': 'Software Engineer'}print(s.format_map(my_dict))print(s.format(**my_dict))
Output:
输出:
My name is Pankaj and I am a Software EngineerMy name is Pankaj and I am a Software Engineer
What if the mapping contains more keys than we actually need in the formatting?
如果映射包含的键数超过了格式化时实际需要的数,该怎么办?
my_dict = {'name': 'Meghna', 'job_title': 'Writer', 'company': 'JournalDev'}print(s.format_map(my_dict))print(s.format(**my_dict))
Output:
输出:
My name is Meghna and I am a WriterMy name is Meghna and I am a Writer
What if the mapping has missing keys?
如果映射缺少键怎么办?
my_dict = {'name': 'Pankaj'}print(s.format_map(my_dict))
Output: KeyError: 'job_title'
输出: KeyError: 'job_title'
Let’s see the output if we use string format() function.
让我们看看如果我们使用字符串format()函数的输出。
print(s.format(**my_dict))
Output: KeyError: 'job_title'
输出: KeyError: 'job_title'
Python string format_map() function is very similar to function. The only difference is that the mappings are used directly and not copied to a . This is useful when the mapping is a dict subclass.
Python字符串format_map()函数与函数非常相似。 唯一的区别是,映射直接使用,而不复制到 。 当映射是dict子类时,这很有用。
If you look at the above examples, the format() and format_map() function behavior is exactly the same. Let’s see how it differs when the mapping is a subclass of dict.
如果查看上述示例,则format()和format_map()函数的行为完全相同。 让我们看看当映射是dict的子类时它有何不同。
class MyDict(dict): def __missing__(self, key): return '#Not Found#'s = 'My name is {name} and I am a {job_title}'my_dict = MyDict(name='Pankaj')print(my_dict)print(s.format_map(my_dict))
Output: My name is Pankaj and I am a #Not Found#
输出: My name is Pankaj and I am a #Not Found#
So when the key is missing, __missing__ function is called and the output is used to replace the substitution.
因此,当缺少键时,将调用__missing__函数,并使用输出替换替换。
Let’s see what happens if we try to use format() function here.
让我们看看如果尝试在此处使用format()函数会发生什么。
print(s.format(**my_dict))
Output: KeyError: 'job_title'
输出: KeyError: 'job_title'
String format() function is throwing an error because it’s copying the provided mappings to a new dict object. So the subclass where we have implemented __missing__ function is never used and hence the error.
字符串format()函数引发错误,因为它正在将提供的映射复制到新的dict对象。 因此,从未使用实现了__missing__函数的子类,因此会出错。
Python String format_map() function is useful when we want to use a dict subclass for mapping purposes. Since the mapping is used directly, the functions from the subclass get called whereas if we use format() function then mappings are copied to a new dict object and our subclass functions will not be called.
当我们要使用dict子类进行映射时,Python String format_map()函数很有用。 由于直接使用映射,因此将调用子类中的函数,而如果使用format()函数,则将映射复制到新的dict对象,并且不会调用子类函数。
Reference:
参考:
翻译自:
转载地址:http://dmmzd.baihongyu.com/