在javascript中打印复杂的python变量

时间:2015-08-20 13:23:26

标签: javascript python json django

我正在尝试在console.log()我的series变量中打印但到目前为止没有成功。

def multiple_series(res):
    matrix = dict(res)
    all_cat_keys = set(key[0] for key in matrix)
    categories = sorted(all_cat_keys)
    all_series_keys = set(key[1] for key in matrix)
    series = [
        {
            'name': series_key,
            'data': [
                [cat_key, matrix.get((cat_key, series_key), 0)]
                for cat_key in categories
            ],
        }
        for series_key in all_series_keys
    ]
    return series

我尝试使用json.dumps()和众所周知的console.log({{ series }}),但我什么都没得到。

我想打印namedata的内容。谁能帮我 ?我没有太多使用JS的经验。感谢

2 个答案:

答案 0 :(得分:1)

console.log({{ series }})无效,因为{{ series }}会被评估为文字。因此JS认为它是一个JS对象。

在对象周围添加引号以将其视为文本,它应该起作用:

console.log("{{ series }}");

答案 1 :(得分:1)

我不确定您的输入数据 - 但是您正在以错误的方式构建系列列表。您应该像这样构建series列表:

def multiple_series(res):
    matrix = dict(res)
    all_cat_keys = set(key[0] for key in matrix)
    categories = sorted(all_cat_keys)
    all_series_keys = set(key[1] for key in matrix)

    series = []
    for series_key in all_series_keys:
        dict_to_add = {'name': series_key}
        data_information = []
        for cat_key in categories:
            data_information.append((cat_key, matrix.get((cat_key, series_key), 0)))
        dict_to_add['data'] = data_information
        series.append(dict_to_add)
    return series