dee*_*eez 2 python dictionary string-interpolation
我有一个字典和一个转换为字符串的日期时间对象.如何在同一行中打印日期时间字符串和字典中的几个项目?
例如:
dictionary = {"_source": {"host": "host", "type": "type"}}
datetime = '25-08-2017 10:26:11'
Run Code Online (Sandbox Code Playgroud)
这就是我要打印的内容:
print("%s %(host)s %(type)s" % (datetime,dictionary["_source"]))
Run Code Online (Sandbox Code Playgroud)
在datetime字符串上获取错误:
TypeError: format requires a mapping
Run Code Online (Sandbox Code Playgroud)
谢谢!
小智 6
一种方法是为日期时间arg指定一个名称:
"{t} {host} {type}".format(t=datetime,**dictionary["_source"])
Run Code Online (Sandbox Code Playgroud)
但实际上即使没有它它也能工作
"{} {host} {type}".format(datetime,**dictionary["_source"])
Run Code Online (Sandbox Code Playgroud)
虽然最好在格式化的字符串imo中使用命名值