将变量从JSON文件加载到Python

2024-10-01 11:27:02 发布

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

我有一个从googlebigquery下载的JSON,它只有一个值。如何在Python脚本中表示这个变量。谢谢你的帮助!在

import smtplib
import os

from slacker import Slacker

def post_slack():
    """Post slack message."""
    try:
        slack = Slacker(token)

        obj = slack.chat.post_message(
            channel='#dataworksapp',
            as_user=True,
            attachments=[
        {
            "fallback": "Required plain-text summary of the attachment.",
            "color": "#36a64f",
            "pretext": "Optional text that appears above the attachment block",
            "author_name": "Data Works",
            "author_link": "http://flickr.com/bobby/",
            "author_icon": "http://flickr.com/icons/bobby.jpg",
            "title": "BlueBox Weekly Information",
            "title_link": "https://console.cloud.google.com/storage/browser/firebase_results/?project=dataworks-356fa",
            "text": "Optional text that appears within the attachment",
            "fields": [
                {
                    "title": "BlueBox Devices Connected",
                    "value": "High",
                    "short": 'true'
                },
                {
                    "title": "BlueBox Connection Time",
                    "value": "High",
                    "short": 'true'
                }
            ],
            "image_url": "http://my-website.com/path/to/image.jpg",
            "thumb_url": "http://example.com/path/to/thumb.png",
            "footer": "Slack API",
            "footer_icon": "https://platform.slack-edge.com/img/default_application_icon.png",
            "ts": 123456789
        }])
        print obj.successful, obj.__dict__['body']['channel'], obj.__dict__[
            'body']['ts']
    except KeyError, ex:
        print 'Environment variable %s not set.' % str(ex)

if __name__ == '__main__':
    post_slack()

这是我的密码。我需要附件中的“value”部分是从JSON上传的变量。在


Tags: thetextimportcomjsonobjhttpattachment