侧边栏壁纸
  • 累计撰写 24 篇文章
  • 累计收到 1 条评论

Python-生成固定时间且间隔为1分随机测试数据Json实例

2023-11-12 / 0 评论 / 94 阅读
温馨提示:
本文最后更新于 2023-11-12,已超过半年没有更新,若内容或图片失效,请留言反馈。

This is a sample Python script.

import random
from datetime import datetime, timedelta
import json

if __name__ == '__main__':
    print_hi('PyCharm')
#### See PyCharm help at https://www.jetbrains.com/help/pycharm/

### 定义开始和结束时间
start_time = datetime(2024, 9, 9, 14, 00)  # 2024年3月1日 8:30
end_time = datetime(2024, 9, 9, 14, 30)  # 2024年3月1日 9:30

### 时间间隔设置为1分
interval = timedelta(minutes=1)

### 生成时间列表
time_list = [start_time + i * interval for i in range((end_time - start_time) // interval)]

print(f"随机产生温度参数:")
for ind, time in enumerate(time_list):
    jsonList = {
        'batteryValue': "100",
        "recordDate": f'{time}',
        "longitude": "114.06455",
        "latitude": "22.54846",
        "sensorList": [
            {
                "sensorId": 1,
                "sensorType": "temperature",
                # 随机生成2.5-7.0之间的小数,保留两位小数 f"{random_number:.2f}"
                "sensorValue": f"{random.uniform(2.5, 7.0):.2f}"
            }
        ],
    }
    end = ''
    if ind < len(time_list) - 1:
        print(json.dumps(jsonList) + ',')
    else:
        print(json.dumps(jsonList))

生成格式:

{"batteryValue": "100", "recordDate": "2024-09-09 14:00:00", "longitude": "114.06455", "latitude": "22.54846", "sensorList": [{"sensorId": 1, "sensorType": "temperature", "sensorValue": "4.50"}]},
{"batteryValue": "100", "recordDate": "2024-09-09 14:01:00", "longitude": "114.06455", "latitude": "22.54846", "sensorList": [{"sensorId": 1, "sensorType": "temperature", "sensorValue": "3.92"}]},
{"batteryValue": "100", "recordDate": "2024-09-09 14:02:00", "longitude": "114.06455", "latitude": "22.54846", "sensorList": [{"sensorId": 1, "sensorType": "temperature", "sensorValue": "2.96"}]},
{"batteryValue": "100", "recordDate": "2024-09-09 14:03:00", "longitude": "114.06455", "latitude": "22.54846", "sensorList": [{"sensorId": 1, "sensorType": "temperature", "sensorValue": "3.18"}]},
{"batteryValue": "100", "recordDate": "2024-09-09 14:04:00", "longitude": "114.06455", "latitude": "22.54846", "sensorList": [{"sensorId": 1, "sensorType": "temperature", "sensorValue": "4.17"}]},
{"batteryValue": "100", "recordDate": "2024-09-09 14:05:00", "longitude": "114.06455", "latitude": "22.54846", "sensorList": [{"sensorId": 1, "sensorType": "temperature", "sensorValue": "3.14"}]},
{"batteryValue": "100", "recordDate": "2024-09-09 14:06:00", "longitude": "114.06455", "latitude": "22.54846", "sensorList": [{"sensorId": 1, "sensorType": "temperature", "sensorValue": "3.88"}]},
{"batteryValue": "100", "recordDate": "2024-09-09 14:07:00", "longitude": "114.06455", "latitude": "22.54846", "sensorList": [{"sensorId": 1, "sensorType": "temperature", "sensorValue": "3.12"}]},
{"batteryValue": "100", "recordDate": "2024-09-09 14:08:00", "longitude": "114.06455", "latitude": "22.54846", "sensorList": [{"sensorId": 1, "sensorType": "temperature", "sensorValue": "2.77"}]},
{"batteryValue": "100", "recordDate": "2024-09-09 14:09:00", "longitude": "114.06455", "latitude": "22.54846", "sensorList": [{"sensorId": 1, "sensorType": "temperature", "sensorValue": "4.34"}]},
{"batteryValue": "100", "recordDate": "2024-09-09 14:10:00", "longitude": "114.06455", "latitude": "22.54846", "sensorList": [{"sensorId": 1, "sensorType": "temperature", "sensorValue": "2.78"}]},
{"batteryValue": "100", "recordDate": "2024-09-09 14:11:00", "longitude": "114.06455", "latitude": "22.54846", "sensorList": [{"sensorId": 1, "sensorType": "temperature", "sensorValue": "4.88"}]},
{"batteryValue": "100", "recordDate": "2024-09-09 14:12:00", "longitude": "114.06455", "latitude": "22.54846", "sensorList": [{"sensorId": 1, "sensorType": "temperature", "sensorValue": "2.79"}]},
{"batteryValue": "100", "recordDate": "2024-09-09 14:13:00", "longitude": "114.06455", "latitude": "22.54846", "sensorList": [{"sensorId": 1, "sensorType": "temperature", "sensorValue": "3.99"}]},
{"batteryValue": "100", "recordDate": "2024-09-09 14:14:00", "longitude": "114.06455", "latitude": "22.54846", "sensorList": [{"sensorId": 1, "sensorType": "temperature", "sensorValue": "4.51"}]},
{"batteryValue": "100", "recordDate": "2024-09-09 14:15:00", "longitude": "114.06455", "latitude": "22.54846", "sensorList": [{"sensorId": 1, "sensorType": "temperature", "sensorValue": "6.85"}]},
{"batteryValue": "100", "recordDate": "2024-09-09 14:16:00", "longitude": "114.06455", "latitude": "22.54846", "sensorList": [{"sensorId": 1, "sensorType": "temperature", "sensorValue": "6.66"}]},
{"batteryValue": "100", "recordDate": "2024-09-09 14:17:00", "longitude": "114.06455", "latitude": "22.54846", "sensorList": [{"sensorId": 1, "sensorType": "temperature", "sensorValue": "3.87"}]},
{"batteryValue": "100", "recordDate": "2024-09-09 14:18:00", "longitude": "114.06455", "latitude": "22.54846", "sensorList": [{"sensorId": 1, "sensorType": "temperature", "sensorValue": "5.65"}]},
{"batteryValue": "100", "recordDate": "2024-09-09 14:19:00", "longitude": "114.06455", "latitude": "22.54846", "sensorList": [{"sensorId": 1, "sensorType": "temperature", "sensorValue": "3.67"}]},
{"batteryValue": "100", "recordDate": "2024-09-09 14:20:00", "longitude": "114.06455", "latitude": "22.54846", "sensorList": [{"sensorId": 1, "sensorType": "temperature", "sensorValue": "6.75"}]},
{"batteryValue": "100", "recordDate": "2024-09-09 14:21:00", "longitude": "114.06455", "latitude": "22.54846", "sensorList": [{"sensorId": 1, "sensorType": "temperature", "sensorValue": "5.25"}]},
{"batteryValue": "100", "recordDate": "2024-09-09 14:22:00", "longitude": "114.06455", "latitude": "22.54846", "sensorList": [{"sensorId": 1, "sensorType": "temperature", "sensorValue": "5.13"}]},
{"batteryValue": "100", "recordDate": "2024-09-09 14:23:00", "longitude": "114.06455", "latitude": "22.54846", "sensorList": [{"sensorId": 1, "sensorType": "temperature", "sensorValue": "3.13"}]},
{"batteryValue": "100", "recordDate": "2024-09-09 14:24:00", "longitude": "114.06455", "latitude": "22.54846", "sensorList": [{"sensorId": 1, "sensorType": "temperature", "sensorValue": "5.72"}]},
{"batteryValue": "100", "recordDate": "2024-09-09 14:25:00", "longitude": "114.06455", "latitude": "22.54846", "sensorList": [{"sensorId": 1, "sensorType": "temperature", "sensorValue": "5.15"}]},
{"batteryValue": "100", "recordDate": "2024-09-09 14:26:00", "longitude": "114.06455", "latitude": "22.54846", "sensorList": [{"sensorId": 1, "sensorType": "temperature", "sensorValue": "4.02"}]},
{"batteryValue": "100", "recordDate": "2024-09-09 14:27:00", "longitude": "114.06455", "latitude": "22.54846", "sensorList": [{"sensorId": 1, "sensorType": "temperature", "sensorValue": "2.90"}]},
{"batteryValue": "100", "recordDate": "2024-09-09 14:28:00", "longitude": "114.06455", "latitude": "22.54846", "sensorList": [{"sensorId": 1, "sensorType": "temperature", "sensorValue": "2.63"}]},
{"batteryValue": "100", "recordDate": "2024-09-09 14:29:00", "longitude": "114.06455", "latitude": "22.54846", "sensorList": [{"sensorId": 1, "sensorType": "temperature", "sensorValue": "2.94"}]}

扫描二维码,在手机上阅读