繁体   English   中英

Python:json.decoder.JSONDecodeError:期望值:第 1 行第 1 列(字符 0)

[英]Python: json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

要求:每 10 分钟提取一次日期范围的 API 数据,这样我就不会得到大量数据并导致 CPU/RAM 问题并具有更好的性能

示例:日期范围是 start_date = '2022-06-05' 和 end_date ='2022-06-06'

所以当我从 API 循环数据时,它应该首先 start_date 和 end_date 如下

start_dt:2022-06-05 00:00:00 end_dt:2022-06-05 00:10:00 
start_dt:2022-06-05 00:10:00 end_dt:2022-06-05 00:20:00 
start_dt:2022-06-05 00:20:00 end_dt:2022-06-05 00:30:00 
start_dt:2022-06-05 00:30:00 end_dt:2022-06-05 00:40:00 



try:
        batch_start_dt = datetime.strptime(date_run, '%Y-%m-%d')
        batch_end_dt =  datetime.strptime(date_run, '%Y-%m-%d')  + timedelta(days=1)
        timedelta_index = pd.date_range(start=batch_start_dt, end=batch_end_dt, freq='10T').to_series()
        for index, value in timedelta_index.iteritems():
            start_dt  = index.to_pydatetime()
            end_dt    = start_dt + pd.Timedelta("20T")

            start_dt2 = start_dt.strftime('%Y-%m-%d %H:%M:%S')
            end_dt2   = end_dt.strftime('%Y-%m-%d %H:%M:%S')
            request_url = ("https://%s:443/api/ChangeList?fromDate=%sZ&endDate=%sZ&timeBufferInSecs=1200&firstLevelFieldsOnly=true"
                        % (api_url,start_dt2.replace(" ", "T"),end_dt2.replace(" ", "T")))
            client.request("GET", request_url, headers=headers)
            response = client.getresponse()
            data = response.read().decode("UTF-8").strip("")
            json_results = json.loads(data)
            json_arr.extend(json_results)
            start_dt += delta
            end_dt += delta

错误:=

 Traceback (most recent call last):
  File "/opt/airflow/dags/date_api.py", line 165, in execute
  File "/opt/airflow/dags/date_api.py", line 129, in call_api
    json_results = json.loads(data)
  File "/usr/local/lib/python3.7/json/__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "/usr/local/lib/python3.7/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/local/lib/python3.7/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

你可以在这里使用 pandas.DateRange :

import pandas as pd

idx = pd.DateRange(start_date, end_date, freq='10M') # Generate 10-minute intervals

for t in idx:
    # ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM