博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python字符串format_map()
阅读量:2530 次
发布时间:2019-05-11

本文共 3389 字,大约阅读时间需要 11 分钟。

python string format_map()
Python string format_map() function returns a formatted version of the string using substitutions from the mapping provided. The substitution arguments are present in the string and identified by curly braces ({ }).

Python字符串format_map()函数使用提供的映射中的替换返回字符串的格式化版本。 替换参数出现在字符串中,并用花括号({})标识。

Python字符串format_map() (Python String format_map())

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'

format_map()与format() (format_map() vs format())

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__函数的子类,因此会出错。

结论 (Conclusion)

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对象,并且不会调用子类函数。

. 签出更多Python示例。

Reference:

参考:

翻译自:

转载地址:http://dmmzd.baihongyu.com/

你可能感兴趣的文章
CVE-2010-2883Adobe Reader和Acrobat CoolType.dll栈缓冲区溢出漏洞分析
查看>>
使用正确的姿势跨域
查看>>
AccountManager教程
查看>>
Android学习笔记(十一)——从意图返回结果
查看>>
算法导论笔记(四)算法分析常用符号
查看>>
ultraedit激活
查看>>
总结(6)--- python基础知识点小结(细全)
查看>>
亿级曝光品牌视频的幕后设定
查看>>
ARPA
查看>>
JSP开发模式
查看>>
我的Android进阶之旅------>Android嵌入图像InsetDrawable的使用方法
查看>>
Detours信息泄漏漏洞
查看>>
win32使用拖放文件
查看>>
Android 动态显示和隐藏软键盘
查看>>
raid5什么意思?怎样做raid5?raid5 几块硬盘?
查看>>
【转】how can i build fast
查看>>
null?对象?异常?到底应该如何返回错误信息
查看>>
django登录验证码操作
查看>>
(简单)华为Nova青春 WAS-AL00的USB调试模式在哪里开启的流程
查看>>
图论知识,博客
查看>>