几个小时后脚本停止

时间:2020-02-15 20:20:48

标签: python json

我正在使用请求并像每11秒那样从API循环获取数据,并将其显示在LCD上。它可以正常运行,但是几个小时后出现json.decoder.jsondecodeerror expecting value line 1 column 1 (char 0)错误,我必须手动重新运行该程序。我不知道如何解决。

这是代码。 api链接显示json格式的公交车位置,并每11秒刷新一次。我在具有1602液晶显示器的raspbian os(raspberry pi 3)上运行它。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import time
import datetime
import requests
import json
import I2C_LCD_driver

# from time import *
mylcd = I2C_LCD_driver.lcd()

turkcec = [
    [0b01110,
     0b10001,
     0b10000,
     0b10000,
     0b10000,
     0b10001,
     0b01110,
     0b00100],
]


def get_data():
    url = "***"
    response = requests.get(url)
    data = response.text
    parsed = json.loads(data)
    list = []
    for i in range(len(parsed["busList"])):
        if parsed["busList"][i]["disabledPerson"] == 0:
            list.append(
                str(parsed["busList"][i]["displayRouteCode"]) + " " + parsed["busList"][i]["stopDiff"] + "drk " +
                parsed["busList"][i]["timeDiff"] + "dk " + "E")
        else:
            list.append(
                str(parsed["busList"][i]["displayRouteCode"]) + " " + parsed["busList"][i]["stopDiff"] + "drk " +
                parsed["busList"][i]["timeDiff"] + "dk")
    return list

data = get_data()
while True:
    starttime = time.time()
    while len(data) > 0:
        #time.sleep(5)
        for x in range(0, len(data), 2):
            mylcd.lcd_clear()
            print(data[x])
            if data[x][0] == "Ç":
                mylcd.lcd_load_custom_chars(turkcec)
                mylcd.lcd_write(0x80)
                mylcd.lcd_write_char(0)
                mylcd.lcd_display_string(data[x][1:], 1, 1)
            else:
                mylcd.lcd_display_string(data[x], 1)
            if x == len(data) - 1:
                time.sleep(11)
                break

            if data[x + 1][0] == "Ç":
                mylcd.lcd_load_custom_chars(turkcec)
                mylcd.lcd_write(0xc0)
                mylcd.lcd_write_char(0)
                mylcd.lcd_display_string(data[x + 1][1:], 2, 1)
            else:
                mylcd.lcd_display_string(data[x + 1], 2)
            print(data[x + 1])
            time.sleep(11)

        if (
                time.time() - starttime) >= 11.0:
            starttime = time.time()
            data = get_data()
            print("--------")
    else:
        mylcd.lcd_clear()
        while True:
            data = get_data()
            mylcd.lcd_display_string(time.strftime("%d/%m/%Y"), 1, 3)
            mylcd.lcd_display_string(time.strftime("%H:%M:%S"), 2, 4)

            time.sleep(1)
            if len(data) > 0:
                break
        continue

这是api链接的结果。我需要来自"busList"的值。

{
  "result": {
    "cmd": "/api/bus/closest",
    "code": 0,
    "message": "OK",
    "dateTime": "20200215145439",
    "debugTimeDiff": "",
    "authType": ""
  },
  "stopInfo": {
    "busStopid": "18",
    "busStopName": "İSKELE",
    "lat": "40.1502179",
    "lng": "26.4031012"
  },
  "busList": [
    {
      "busId": "17059",
      "plate": "17-H-0059",
      "disabledPerson": "0",
      "displayRouteCode": "Ç3",
      "displayName": "Ç-3",
      "headSign": "Ç-3",
      "routeCode": "70003",
      "shapeId": "700030",
      "stopDiff": "2",
      "timeDiff": "1",
      "lat": "40.14762",
      "lng": "26.406181"
    },
    {
      "busId": "17070",
      "plate": "17-H-0070",
      "disabledPerson": "0",
      "displayRouteCode": "Ç8",
      "displayName": "Ç-8",
      "headSign": "Ç-8",
      "routeCode": "70008",
      "shapeId": "700080",
      "stopDiff": "2",
      "timeDiff": "5",
      "lat": "40.14622",
      "lng": "26.40825"
    },
    {
      "busId": "17076",
      "plate": "17-H-0076",
      "disabledPerson": "0",
      "displayRouteCode": "Ç1",
      "displayName": "Ç-1",
      "headSign": "Ç-1",
      "routeCode": "70001",
      "shapeId": "700010",
      "stopDiff": "3",
      "timeDiff": "9",
      "lat": "40.14187",
      "lng": "26.40788"
    },
    {
      "busId": "17034",
      "plate": "17-H-0034",
      "disabledPerson": "0",
      "displayRouteCode": "Ç7",
      "displayName": "Ç-7",
      "headSign": "Ç-7",
      "routeCode": "70007",
      "shapeId": "700070",
      "stopDiff": "9",
      "timeDiff": "14",
      "lat": "40.132263",
      "lng": "26.409042"
    },
    {
      "busId": "17029",
      "plate": "17-H-0029",
      "disabledPerson": "0",
      "displayRouteCode": "Ç3",
      "displayName": "Ç-3",
      "headSign": "Ç-3",
      "routeCode": "70003",
      "shapeId": "700030",
      "stopDiff": "5",
      "timeDiff": "14",
      "lat": "40.145737",
      "lng": "26.4206"
    },
    {
      "busId": "17018",
      "plate": "17-H-0018",
      "disabledPerson": "0",
      "displayRouteCode": "ÇT3",
      "displayName": "ÇT3",
      "headSign": "ÇT3",
      "routeCode": "70016",
      "shapeId": "700160",
      "stopDiff": "5",
      "timeDiff": "15",
      "lat": "40.14575",
      "lng": "26.420376"
    },
    {
      "busId": "17042",
      "plate": "17-H-0042",
      "disabledPerson": "0",
      "displayRouteCode": "11K",
      "displayName": "11K",
      "headSign": "11K",
      "routeCode": "70012",
      "shapeId": "700120",
      "stopDiff": "9",
      "timeDiff": "19",
      "lat": "40.12353",
      "lng": "26.410328"
    }
  ],
  "routeList": [
    {
      "routeCode": "70004",
      "displayRouteCode": "Ç4",
      "name": "Ç-4",
      "headSign": "KOCATEPE CAMİ",
      "stopArrivalTime": ""
    },
    {
      "routeCode": "70005",
      "displayRouteCode": "Ç5",
      "name": "Ç-5",
      "headSign": "SSK Durağı",
      "stopArrivalTime": ""
    },
    {
      "routeCode": "70011",
      "displayRouteCode": "11G",
      "name": "11G",
      "headSign": "SSK Durağı",
      "stopArrivalTime": ""
    },
    {
      "routeCode": "71016",
      "displayRouteCode": "960",
      "name": "960",
      "headSign": "Eski Hastane",
      "stopArrivalTime": ""
    },
    {
      "routeCode": "72011",
      "displayRouteCode": "11G-T",
      "name": "11G",
      "headSign": "NUSRET YURDU",
      "stopArrivalTime": ""
    }
  ]
}

这是错误。

Traceback (most recent call last):
  File "/home/pi/Desktop/xxx/xxx.py", line x, in <module>
    data = get_data()
  File "/home/pi/Desktop/xxx/xxx.py", line, in get_data
    parsed = json.loads(data)
  File "/usr/lib/python3.5/json/__init__.py", line 319, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.5/json/decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.5/json/decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

我找到了解决方案。谢谢

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import time
import datetime
import requests
import json
import I2C_LCD_driver

# from time import *
mylcd = I2C_LCD_driver.lcd()

turkcec = [
    [0b01110,
     0b10001,
     0b10000,
     0b10000,
     0b10000,
     0b10001,
     0b01110,
     0b00100],
]
while True:
    try:
    def get_data():
        url = "***"
        response = requests.get(url)
        data = response.text
        parsed = json.loads(data)
        list = []
        for i in range(len(parsed["busList"])):
            if parsed["busList"][i]["disabledPerson"] == 0:
                list.append(
                    str(parsed["busList"][i]["displayRouteCode"]) + " " + parsed["busList"][i]["stopDiff"] + "drk " +
                    parsed["busList"][i]["timeDiff"] + "dk " + "E")
            else:
                list.append(
                    str(parsed["busList"][i]["displayRouteCode"]) + " " + parsed["busList"][i]["stopDiff"] + "drk " +
                    parsed["busList"][i]["timeDiff"] + "dk")
        return list


    data = get_data()
    while True:
        starttime = time.time()
        while len(data) > 0:
            # time.sleep(5)
            for x in range(0, len(data), 2):
                mylcd.lcd_clear()
                print(data[x])
                if data[x][0] == "Ç":
                    mylcd.lcd_load_custom_chars(turkcec)
                    mylcd.lcd_write(0x80)
                    mylcd.lcd_write_char(0)
                    mylcd.lcd_display_string(data[x][1:], 1, 1)
                else:
                    mylcd.lcd_display_string(data[x], 1)
                if x == len(data) - 1:
                    time.sleep(11)
                    break

                if data[x + 1][0] == "Ç":
                    mylcd.lcd_load_custom_chars(turkcec)
                    mylcd.lcd_write(0xc0)
                    mylcd.lcd_write_char(0)
                    mylcd.lcd_display_string(data[x + 1][1:], 2, 1)
                else:
                    mylcd.lcd_display_string(data[x + 1], 2)
                print(data[x + 1])
                time.sleep(11)

            if (
                    time.time() - starttime) >= 11.0:
                starttime = time.time()
                data = get_data()
                print("--------")
        else:
            mylcd.lcd_clear()
            while True:
                data = get_data()
                mylcd.lcd_display_string(time.strftime("%d/%m/%Y"), 1, 3)
                mylcd.lcd_display_string(time.strftime("%H:%M:%S"), 2, 4)

                time.sleep(1)
                if len(data) > 0:
                    break
            continue
    except:
        if (time.time() - starttime) >= 11.0:
            continue

0 个答案:

没有答案