zoukankan      html  css  js  c++  java
  • 元组套元组转列表套字典数据格式

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    import json
    
    
    # 原始数据
    rows = (('apollo', 'male', '164.jpeg'), ('apollo', 'male', ''))
    # 表头
    names = 'username gender pic'.split()
    # URL公共部分
    fs_url = 'http://www.baidu.com/'
    
    # 新数据列表
    L = []
    for e in rows:
        L1 = list(e)
        pic = e[2]
        if pic == '':
            L1[2] = "%suser_pic/default.jpg" % (fs_url)
        else:
            L1[2] = "%suser_pic/small_%s" % (fs_url, pic)
        L.append(L1)
    print L
    # 用zip组合列表套字典
    """
    [{'username': 'apollo', 'gender': 'male', 'pic': 'http://www.baidu.com/user_pic/small_164.jpeg'}, 
    {'username': 'apollo', 'gender': 'male', 'pic': 'http://www.baidu.com/user_pic/default.jpg'}]
    """
    data = [dict(zip(names, d)) for d in L]
    print data
    # 对上面结果Json序列化
    """
    [{"username": "apollo", "gender": "male", "pic": "http://www.baidu.com/user_pic/small_164.jpeg"}, 
     {"username": "apollo", "gender": "male", "pic": "http://www.baidu.com/user_pic/default.jpg"}]
     """
    info = json.dumps(data, ensure_ascii=False)
    print info
    # 组合返回结果,返给前端
    """
    response = {
            "errcode": 0,
            "errmsg": "获取用户列表成功",
            "readLog":[{"username": "apollo", "gender": "male", "pic": "http://www.baidu.com/user_pic/small_164.jpeg"},
                       {"username": "apollo", "gender": "male", "pic": "http://www.baidu.com/user_pic/default.jpg"}]
            }
    """
    response = """
        {
        "errcode": 0,
        "errmsg": "获取用户列表成功",
        "readLog":%s
        }
        """ % (info)
    
    print response
    
  • 相关阅读:
    谷歌浏览器解决跨域
    vue 解决跨域问题
    nth-of-type & nth-child 的区别
    uniapp 小程序 获取位置信息
    笔记本使用命令创建wifi
    express每次修改后重新启动
    express 一个js文件中写多个路由 然后使用
    小程序分享到朋友圈
    小程序分享给朋友
    小程序客服功能实现
  • 原文地址:https://www.cnblogs.com/apollo1616/p/10418153.html
Copyright © 2011-2022 走看看