Отформатируйте вывод из pymysql - PullRequest
0 голосов
/ 17 июня 2020

У меня есть такие данные

| 14012 | 1004         | emp         | neutral  | cam01  | out         | 2020-01-27 15:37:22 | 2020-01-27 16:03:16 |
| 14013 | 1003         | emp         | neutral  | cam02  | out         | 2020-01-20 19:18:46 | 2020-01-20 19:18:48 |
| 14014 | 1003         | emp         | negetive | cam01  | out         | 2020-02-12 12:30:02 | 2020-02-12 13:06:59 |
| 14015 | 1004         | emp         | negetive | cam04  | in          | 2020-02-05 17:07:00 | 2020-02-05 17:07:56 |
| 14016 | 1000         | emp         | positive | cam05  | out         | 2020-01-28 08:29:41 | 2020-01-28 08:32:55 |
| 14017 | 1000         | emp         | neutral  | cam02  | out         | 2020-01-27 14:24:20 | 2020-01-27 14:31:52 |
| 14018 | 1003         | emp         | neutral  | cam05  | out         | 2020-01-28 15:28:06 | 2020-01-28 15:33:20 |
| 14019 | 1002         | emp         | neutral  | cam01  | in          | 2020-02-07 14:04:15 | 2020-02-07 14:23:28 |
| 14020 | 1000         | emp         | neutral  | cam02  | out         | 2020-01-30 17:04:10 | 2020-01-30 17:08:10 |
| 14021 | 1002         | emp         | positive | cam02  | in          | 2020-01-20 18:13:10 | 2020-01-20 18:13:14 |
| 14022 | 1002         | emp         | positive | cam04  | out         | 2020-01-20 13:20:42 | 2020-01-20 13:23:04 |
| 14023 | 1003         | emp         | neutral  | cam04  | in          | 2020-02-01 18:10:40 | 2020-02-01 18:11:02 |
| 14024 | 1002         | emp         | positive | cam01  | out         | 2020-01-17 06:05:31 | 2020-01-17 06:06:37 |
| 14025 | 1005         | emp         | neutral  | cam03  | out         | 2020-02-08 15:35:31 | 2020-02-08 15:45:55 |
| 14026 | 1004         | emp         | positive | cam02  | in          | 2020-01-24 19:46:08 | 2020-01-24 20:01:11 |
| 14027 | 1001         | emp         | neutral  | cam00  | out         | 2020-01-28 07:12:40 | 2020-01-28 07:14:08 |
| 14028 | 1002         | emp         | positive | cam00  | out         | 2020-02-05 17:34:15 | 2020-02-05 17:35:37 |
| 14029 | 1003         | emp         | neutral  | cam03  | out         | 2020-01-30 14:54:27 | 2020-01-30 14:57:58 |
| 14030 | 1005         | emp         | neutral  | cam00  | in          | 2020-01-30 12:10:48 | 2020-01-30 12:12:02 |
| 14031 | 1002         | emp         | neutral  | cam02  | out         | 2020-02-06 19:30:55 | 2020-02-06 19:32:06 |
| 14032 | 1002         | emp         | neutral  | cam04  | out         | 2020-01-25 06:13:32 | 2020-01-25 06:17:13 |
| 14033 | 1002         | emp         | neutral  | cam00  | in          | 2020-01-28 15:02:43 | 2020-01-28 15:06:51 |
| 14034 | 1004         | emp         | neutral  | cam00  | out         | 2020-01-20 08:54:29 | 2020-01-20 08:58:04 |
| 14035 | 1003         | emp         | negetive | cam05  | in          | 2020-02-14 12:48:50 | 2020-02-14 12:52:21 |
| 14036 | 1003         | emp         | neutral  | cam04  | in          | 2020-02-05 09:21:30 | 2020-02-05 09:30:55 |
| 14037 | 1002         | emp         | negetive | cam02  | out         | 2020-02-10 14:32:28 | 2020-02-10 14:46:26 |
| 14038 | 1004         | emp         | neutral  | cam04  | in          | 2020-01-16 15:49:52 | 2020-01-16 15:53:26 |

Мне нужно вывести вот так: {data: [{label: «8AM», value: «15»}, {label: «9AM», value: "25"}, {label: "10AM", value: "30"}]} и это мой код, проблема в том, что он показывает значение для всех из них 0. Мне трудно понять, как для форматирования ответов sql

from flask import Flask
from flask import request, jsonify
from datetime import datetime
import pymysql

app = Flask(__name__)

host = "localhost"
user = "test"
password = "test123"
db = "test"
con = pymysql.connect(host=host, user=user, password=password, db=db, cursorclass=pymysql.cursors.DictCursor)
cur = con.cursor()


@app.route('/getNumberOfPeoplesInPaticularNode/<string:date>/<string:cameraId>', methods=['GET'])
def getNumberOfPeoplesInPaticularNode(date,cameraId):
    date = datetime.strptime(date, '%Y-%m-%d %H:%M:%S')
    ls=[]
    fd={}
    for i in range(0,23):
        cur.execute("Select COUNT(*) value from face_log where timestamp = %s and camera = %s and hour(%s) = %s" ,(date,cameraId,date,i))
        rows=cur.fetchall()
        d={}
        d["label"]=i
        d.update(rows[0])
        ls.append(d)
    fd["data"]=ls       
    return fd

My Output для

http://127.0.0.1:5000/getNumberOfPeoplesInPaticularNode/2020-01-28%2015:54:55/cam01

Это в основном просто занимает указанный час, а не другие часы дня

{
  "data": [
    {
      "label": 0, 
      "value": 0
    }, 
    {
      "label": 1, 
      "value": 0
    }, 
    {
      "label": 2, 
      "value": 0
    }, 
    {
      "label": 3, 
      "value": 0
    }, 
    {
      "label": 4, 
      "value": 0
    }, 
    {
      "label": 5, 
      "value": 0
    }, 
    {
      "label": 6, 
      "value": 0
    }, 
    {
      "label": 7, 
      "value": 0
    }, 
    {
      "label": 8, 
      "value": 0
    }, 
    {
      "label": 9, 
      "value": 0
    }, 
    {
      "label": 10, 
      "value": 0
    }, 
    {
      "label": 11, 
      "value": 0
    }, 
    {
      "label": 12, 
      "value": 0
    }, 
    {
      "label": 13, 
      "value": 0
    }, 
    {
      "label": 14, 
      "value": 0
    }, 
    {
      "label": 15, 
      "value": 16
    }, 
    {
      "label": 16, 
      "value": 0
    }, 
    {
      "label": 17, 
      "value": 0
    }, 
    {
      "label": 18, 
      "value": 0
    }, 
    {
      "label": 19, 
      "value": 0
    }, 
    {
      "label": 20, 
      "value": 0
    }, 
    {
      "label": 21, 
      "value": 0
    }, 
    {
      "label": 22, 
      "value": 0
    }, 
    {
      "label": 23, 
      "value": 0
    }
  ]
}
...