首页 > 解决方案 > 在python中打印布尔值

问题描述

我有一个 json 输入

{
     "registryNo":2222904913,
     "resgistrySource":"C22",
      "DateTime":"None",
   
         "Payments":[{
                      "Paymentdetail":[
                                     {
                                     "amount":359.95,
                                     "currencyCode":"GBP",
   

需要生成一个布尔输出来检查货币代码字段是否具有 GBP 值。如果是,则将货币值打印为“True”,否则打印“False”

这是我写的

if "currencycode" in datastore(["Payments"][0]["paymentdetail"]) == "GBP":
     print("currencyvalue") = "True" else "FALSE"

——这似乎不起作用。

标签: python

解决方案


如果我很了解您的问题,您可以尝试这样做:

if "currencycode" in datastore(["Payments"][0]["paymentdetail"][0]):

    if datastore(["Payments"][0]["paymentdetail"][0]["currencycode"]) == "GBP":
        print("currencyvalue = True")
    else:
        print("currencyvalue = false")

如果您只想打印“True”或“False”,请执行以下操作:

if "currencycode" in datastore(["Payments"][0]["paymentdetail"][0]):

    print(datastore(["Payments"][0]["paymentdetail"][0]["currencycode"]))

对于时间戳,您可以这样做:

from datetime import datetime

dt_object = datetime.fromtimestamp(your_valid_timestamp)
print(dt_object)

推荐阅读