zoukankan      html  css  js  c++  java
  • Python3-笔记-E-012-库-JSON

    import json, pprint

    # 支持的类型:字典、列表、整型、浮点型、字符串、布尔型或None
    def LoadsAndDumps():
    stringOfJsonData = '{"name": "Zophie", "isCat": true, "miceCaught": 0, "felineIQ": null}'
    # 返回字典
    data = json.loads(stringOfJsonData)
    pprint.pprint(data)
    # 返回json
    jsondata = json.dumps(data)
    print(jsondata)



    # 测试读取天气预报
    import requests, pprint, json
    def fetchWeathernow(location):
    result = requests.get('https://api.seniverse.com/v3/weather/now.json?', params={
    'key': 'xo4gfeq107zexjat',
    'location': location,
    'language': 'zh-Hans',
    'unit': 'c',
    }, timeout=1)
    return result.text
    def fetchWeatherdaily(location):
    result = requests.get('https://api.seniverse.com/v3/weather/daily.json?', params={
    'key': 'xo4gfeq107zexjat',
    'location': location,
    'language': 'zh-Hans',
    'unit': 'c',
    'start': 0,
    'days': 5
    }, timeout=1)
    return result.text

    if __name__ == '__main__':
    location = 'tianjin'
    result = fetchWeathernow(location)
    datanow = json.loads(result)
    now = datanow['results'][0]['now']
    city = datanow['results'][0]['location']['name']
    print('%s现在的天气是%s %s摄氏度' % (city, now['text'], now['temperature']))
    result = fetchWeatherdaily(location)
    datadaily = json.loads(result)
    lst = datadaily['results'][0]['daily']
    city = datadaily['results'][0]['location']['name']
    print('%s近三天的天气' % city)
    for m in lst:
    print('%s 白天:%s,夜晚:%s,最高温度:%s,最低温度:%s,风向:%s,风力:%s' %
    (m['date'],
    m['text_day'],
    m['text_night'],
    m['high'],
    m['low'],
    m['wind_direction'],
    m['wind_scale']
    ))
     
  • 相关阅读:
    【2014广州市选day1】JZOJ2020年9月12日提高B组T2 导弹拦截
    JZOJ2020年9月12日提高B组反思
    部署zookeeper
    13安装heapster
    11 安装traefik
    10 安装coredns
    9 安装flannel
    8 部署kube-proxy
    7 部署kubelete
    6 部署 controller-manager scheduler
  • 原文地址:https://www.cnblogs.com/vito13/p/7815013.html
Copyright © 2011-2022 走看看