目前,我很难理解为什么以下代码不起作用:
import json
import random
def _time_series_prc(start_date, count, periodicity, is_history=True):
values = []
print(type(start_date))
print(type(periodicity))
for i in range(count - 1):
value = random.uniform(0, 1)
values.append(value)
return _build_series(values, start_date, periodicity, is_history)
def _build_series(values, start_date, periodicity, is_history):
if is_history:
values.reverse()
return {
'periodicity': periodicity,
'startDate': start_date,
'values': values,
}
result = _time_series_prc('2019-07-17', 52, 'WEEKLY')
print(json.dumps(result, indent=4));产出:
<class 'str'>
<class 'str'>
TypeError: Object of type function is not JSON serializable在第7行,我得到的json.dumps错误: TypeError: Object的类型函数不是JSON序列化`。我不太长时间使用ptyhon,但我不明白这怎么可能是一个函数指针而不是返回的值。
发布于 2019-07-18 06:08:05
您的代码在Python3.7.3上适用于我。
还请注意,for i in range(count - 1):将给您51个结果,而不是本例中的52个(print(len(result['values'])))。还可以尝试删除最后一行代码中的;,以防万一:)
https://stackoverflow.com/questions/57087839
复制相似问题