python - 将float字符串转换为json中的float

标签 python json

我有一个包含以下数据的 json(test.json) 文件。我有大约 10000 条记录。我需要将 value 从字符串转换为浮点写入新文件 (test1.json)。我如何从 Python 执行此操作?

{
    "name":"test001",
    "cat":"test",
    "loc":"x loc",
    "ings":[
        {
            "name":"rrrrrr",
            "value":"13.0"
        },
        {
            "name":"hhhh",
            "value":"18.0"
        }
    ],
    "nums":[
        {
            "name":"kkkk",
            "value":"82.05"
        },
        {
            "name":"uuuuu",
            "value":"53.55"
        }
    ]
},
{
    "name":"test002",
    "cat":"test1",
    "loc":"y loc",
    "ings":[
        {
            "name":"trtrtr",
            "value":"11.0"
        },
        {
            "name":"wewew",
            "value":"19.0"
        }
    ],
    "nums":[
        {
            "name":"iuyt",
            "value":"122.05"
        },
        {
            "name":"oiui",
            "value":"15.5"
        }
    ]
}

生成的 json 文件(test1.json)应该如下所示...

{
    "name":"test001",
    "cat":"test",
    "loc":"x loc",
    "ings":[
        {
            "name":"rrrrrr",
            "value":13.0
        },
        {
            "name":"hhhh",
            "value":18.0
        }
    ],
    "nums":[
        {
            "name":"kkkk",
            "value":82.05
        },
        {
            "name":"uuuuu",
            "value":53.55
        }
    ]
},
{
    "name":"test002",
    "cat":"test1",
    "loc":"y loc",
    "ings":[
        {
            "name":"trtrtr",
            "value":11.0
        },
        {
            "name":"wewew",
            "value":19.0
        }
    ],
    "nums":[
        {
            "name":"iuyt",
            "value":122.05
        },
        {
            "name":"oiui",
            "value":15.5
        }
    ]
}

最佳答案

您可以为 json.loads 提供一个 object_hook允许您修改在 json 中找到的任何对象(字典)的方法:

import json

json_data = """
[{
    "name":"test001",
    "cat":"test",
    "loc":"x loc",
    "ings":[
        {
            "name":"rrrrrr",
            "value":"13.0"
        },
        {
            "name":"hhhh",
            "value":"18.0"
        }
    ],
    "nums":[
        {
            "name":"kkkk",
            "value":"82.05"
        },
        {
            "name":"uuuuu",
            "value":"53.55"
        }
    ]
},
{
    "name":"test002",
    "cat":"test1",
    "loc":"y loc",
    "ings":[
        {
            "name":"trtrtr",
            "value":"11.0"
        },
        {
            "name":"wewew",
            "value":"19.0"
        }
    ],
    "nums":[
        {
            "name":"iuyt",
            "value":"122.05"
        },
        {
            "name":"oiui",
            "value":"15.5"
        }
    ]
}]
"""

def as_float(obj):
    """Checks each dict passed to this function if it contains the key "value"
    Args:
        obj (dict): The object to decode

    Returns:
        dict: The new dictionary with changes if necessary
    """
    if "value" in obj:
        obj["value"] = float(obj["value"])
    return obj


if __name__ == '__main__':
    l = json.loads(json_data, object_hook=as_float)
    print (json.dumps(l, indent=4))

这会产生您想要的结果:

[
    {
        "loc": "x loc",
        "ings": [
            {
                "name": "rrrrrr",
                "value": 13.0
            },
            {
                "name": "hhhh",
                "value": 18.0
            }
        ],
        "name": "test001",
        "nums": [
            {
                "name": "kkkk",
                "value": 82.05
            },
            {
                "name": "uuuuu",
                "value": 53.55
            }
        ],
        "cat": "test"
    },
    {
        "loc": "y loc",
        "ings": [
            {
                "name": "trtrtr",
                "value": 11.0
            },
            {
                "name": "wewew",
                "value": 19.0
            }
        ],
        "name": "test002",
        "nums": [
            {
                "name": "iuyt",
                "value": 122.05
            },
            {
                "name": "oiui",
                "value": 15.5
            }
        ],
        "cat": "test1"
    }
]

改为写入文件:

with open("out.json", "w+") as out:
    json.dump(l, out, indent=4)

关于python - 将float字符串转换为json中的float,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36672886/

相关文章:

python - 如果 X 或 Y 或 Z 则使用 *that* 一个?

python - pandas groupby 到嵌套的 json——不需要计算字段

python - 使用其他嵌入的 JSON 对象展平 JSON 对象

php - 在 PHP 中检测 json_decode() 失败

cocoa - 如何导入JSON框架?

python - 在 json 对象中搜索 key

Python Azure 函数 - 使用 Key Vault 进行 MSI 身份验证

python - 对 imports 和 evals 感到困惑——Python

python - numpy.ndarray : converting to a "normal" class

json - Play Map[Int,_] 的 JSON 格式化程序