我有一个字典和一个日期时间对象,我转换为字符串。如何从同一行中的字典中打印日期时间字符串和多个项目?字符串和字典在同一行的Python字符串插值
例如:
dictionary = {"_source": {"host": "host", "type": "type"}}
datetime = '25-08-2017 10:26:11'
这就是我想要打印:
print("%s %(host)s %(type)s" % (datetime,dictionary["_source"]))
的时间字符串得到一个错误:
TypeError: format requires a mapping
谢谢!
你可以添加你是什么什么这个例子输出? '25 -08-2017 10:26:11主机类型'? – ands
这里是你如何使用'format'来完成它:'print(“{0} {1 [host]} {1 [type]}”。format(datetime,dictionary [“_ source”]))'Read more about'格式'[here](https://docs.python.org/3.4/library/string.html#formatspec) –