【问题标题】:Load variable from JSON file to Python [closed]将变量从 JSON 文件加载到 Python [关闭]
【发布时间】:2017-12-21 11:37:08
【问题描述】:

我有一个从 Google BigQuery 下载的 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()

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

【问题讨论】:

  • 请发布一些代码,以便我们为您提供帮助!
  • 我在编辑中添加了我的代码。谢谢
  • 处理 json 数据有据可查,网上的例子数不胜数。问之前你做了什么研究?见How much research effort is expected of Stack Overflow users?
  • 我之前已经成功加载了一个 JSON,但这与从 JSON 加载一个值并将这个值保存为变量以在我的代码中的其他位置使用相同吗?

标签: python json


【解决方案1】:
import json
json_text = '{"some":"json"}'
some_dict = json.loads(json_text)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-07
    • 1970-01-01
    • 2018-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-01
    相关资源
    最近更新 更多