在python中打印布尔值

2024-10-03 17:26:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个json输入

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

需要生成布尔输出以检查货币代码字段是否具有GBP值。如果是,则将货币值打印为“真”或“假”

这是我写的

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

这似乎不起作用


Tags: 代码nonejsondatetimeif货币amountgbp
2条回答

由于==运算符本身返回TrueFalse值,因此无需再次为其赋值

currencycalue = datastore['Payments'][0]['Paymentdetail'][0]['currencyCode']=="GBP"
print(currencyvalue)

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

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

    if datastore(["Payments"][0]["paymentdetail"][0]["currencycode"]) == "GBP":
        print("currencyvalue = True")
    else:
        print("currencyvalue = 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)

相关问题 更多 >