动态 版块 发帖 消息 我的
xwylt
书山有路勤为径,学海无涯苦作舟
lytcreateLv5   
django导出json实例     
def ceshi(request):
    if request.method == 'GET':
        conn = pymysql.connect(host=host, port=port, user=user, password=password, database=database, charset='utf8')
        cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
        cursor.execute("select * from auth_user")
        rows = cursor.fetchall()

        rowarray_list = []
        for row in rows:
            t = (row['id'], row['password'], row['is_superuser'], row['username'])
            rowarray_list.append(t)
        g = json.dumps(rowarray_list)
        with open("F:\student_rowarrays.json", "w") as h:
            h.write(g)

        objects_list = []
        for row in rows:
            d = collections.OrderedDict()
            d["id"] = row['id']
            d["password"] = row['password']

            d["is_superuser"] = row['is_superuser']
            d["username"] = row['username']


            objects_list.append(d)
        print(objects_list)
        for i in range(0,len(objects_list)):
            j = json.dumps(objects_list[i])



            with open("F:\student_objects.json", "a") as f:
                f.write(j)

        conn.close()
        return render(request,'ceshi.html')

    else:
        return render(request,'ceshi.html')

 0  已被阅读了88次  楼主 2020-10-11 18:30:06
回复列表

回复:django导出json实例

Powered by Xwylt.com
© 2020-2020 lytcreate
您的IP:54.236.35.159,2020-10-31 03:12:50,Processed in 0.00732 second(s).
Powered by HadSky
书山有路勤为径,学海无涯苦作舟
已有0次打赏