首页 > 解决方案 > JSON到python中的字符串

问题描述

在我的项目中有一部分使用 python,我在其中获取 JSON 对象并获取这些值并将它们转换为字符串。

我的输入将是:

data = {
        "date":'12.04.2019',
        "time":'06 am',
        "event":'Complete project'
        }

我的输出应该是:

On 12.04.2019, 06 am the reminder is to Complete project

我尝试了不同的方法将 JSON 值转换为字符串,但出现了某种错误。

标签: pythonstring

解决方案


从 python 3.6 开始,您可以使用 f 字符串:

print(f"On {data['date']}, {data['time']} the reminder is to {data['event']}")

如果您没有 python 3.6 或更高版本,我建议您使用格式化:

print("On %s, %s the reminder is to %s" %(data['date'], data['time'], data['event']))

推荐阅读