Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python TypeError:函数类型的对象不可JSON序列化_Python - Fatal编程技术网

Python TypeError:函数类型的对象不可JSON序列化

Python TypeError:函数类型的对象不可JSON序列化,python,Python,我目前很难理解为什么以下代码不起作用: 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.uni

我目前很难理解为什么以下代码不起作用:

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

TypeError:函数类型的对象不可JSON序列化

在第7行,
json.dumps
I得到一个错误:TypeError:type函数的对象不是json可序列化的`。我使用ptyhon的时间不长,但我不明白这怎么可能是函数指针而不是返回值。

您的代码在Python 3.7.3上对我有效


还要注意,对于范围内的i(计数-1):将给出51个结果,而不是本例中的52个结果(
print(len(result['values')))
)。同时尝试删除
在您的最后一行代码中以防万一:)

我们需要查看
开始日期
周期性
。。。以及它们的类型:
print(类型(开始日期))
print(类型(周期))
。您可能无法调用以下函数:
periodicy()
start\u date()
。如果您实际上是要序列化一个函数以供以后使用,请考虑<代码>泡菜< /代码>,而不是<代码> JSON<代码> >从BuugDySo系列返回的值是一个函数。可能是周期性的。添加了类型的打印。他们都是string@Mulgard您的代码适用于me@Mulgard你能再次运行你的代码吗