如何将列表传递到SNS消息中?

时间:2019-01-16 09:25:14

标签: python amazon-web-services amazon-sns

我正在尝试使用python发送SNS。一切正常。但是我想将列表传递到SNS的消息正文中。该怎么做?

我已经尝试过,但是没有工作。

list = [('9',), ('8',), ('7',), ('6',), ('5',), ('4',), ('3',), ('23',), ('22',), ('21',), ('20',), ('2',), ('19',), ('18',), ('17',), ('16',), ('15',), ('14',), ('13',), ('12',), ('11',), ('10',), ('0',)]

message = { "source": "datapipeline",
            "application_nm": "omni",
            "severity": "2",
            "batch_run_start_dttm": "$actualStartTime",
            "batch_run_end_dttm": "actualEndTime",
            "batch_run_status_cd": "failed",
            "job_orchestration_id": "NA",
            "stage_nm": "NA",
            "stage_start_dttm": "NA",
            "stage_end_dttm": "NA",
            "stage_status_cd":  "failed",
            "job_run_id": "NA",
            "source_nm": "NA",
            "target_nm": "NA",
            "source_details": "NA",
            "target_details": "NA",
            "msg": "These mentioned hours having difference more than 50% from previous week:" +"%s"  ,
            "counts":"0",
            "notes": "After hourly comparison check, the above mentioned hours is having difference more than 50%"
          } %list

try:
    response = client.publish(
    TargetArn=sns_arn,
    Subject = 'Hourly comaprison mismatch',
    Message=json.dumps({'default': json.dumps(message)}),
    MessageStructure='json'
    )
    #return 'servicenow'
except Exception as e:
    return e
    print(e)
    print('SNS is throwing error!')

试图传递的列表值为%s的味精。但是抛出一个错误。如何将该列表值传递到SNS的JSON格式味精正文中?

1 个答案:

答案 0 :(得分:0)

尝试一下:

message = { "source": "datapipeline",
            "application_nm": "omni",
            "severity": "2",
            "batch_run_start_dttm": "$actualStartTime",
            "batch_run_end_dttm": "actualEndTime",
            "batch_run_status_cd": "failed",
            "job_orchestration_id": "NA",
            "stage_nm": "NA",
            "stage_start_dttm": "NA",
            "stage_end_dttm": "NA",
            "stage_status_cd":  "failed",
            "job_run_id": "NA",
            "source_nm": "NA",
            "target_nm": "NA",
            "source_details": "NA",
            "target_details": "NA",
            "msg": "These mentioned hours having difference more than 50% from previous week:" +"%s"  ,
            "counts":"0",
            "notes": "After hourly comparison check, the above mentioned hours is having difference more than 50%",
            "list": list # this is how to do it
          }

顺便说一句,将变量命名为“列表”是一个非常糟糕的主意,该变量与内置的list()函数冲突。