在Python中将列表从Json转换为Float

时间:2018-06-25 03:21:14

标签: python json list pandas

我有一个问题,我想从json文件转换我的列表。当我尝试以相同的格式转换较小的文件时,效果很好,正如我预期的那样。但是,当我用更大的文件和相同的格式更改数据时,会出现如下错误:

无法将字符串转换为浮点数:'-2.942-2.942',但是在我检查后,我的较大json文件中没有字符串'-2.942-2.942'。所以我的问题是:RAM内存是否有问题,因此我的计算机无法将列表转换为浮动状态?

我有以下JSON数据:

    {
    "En": -2.942,
    "atoms": [
        {
            "type": "Br",
            "xyz": [
                -4.0223,
                0.5054,
                -0.022
            ]
        },
        {
            "type": "Cl",
            "xyz": [
                3.9221,
                0.4837,
                0.009
            ]
        },
        {
            "type": "N",
            "xyz": [
                0.0218,
                -0.5066,
                -0.0031
            ]
        },
        {
            "type": "C",
            "xyz": [
                -1.1862,
                0.3061,
                0.0071
            ]
        },
        {
            "type": "C",
            "xyz": [
                1.2137,
                0.3338,
                0.0107
            ]
        },
        {
            "type": "C",
            "xyz": [
                -2.4113,
                -0.5886,
                0.0255
            ]
        },
        {
            "type": "C",
            "xyz": [
                2.4622,
                -0.5338,
                -0.0271
            ]
        },
        {
            "type": "H",
            "xyz": [
                -1.2118,
                0.9549,
                -0.8777
            ]
        },
        {
            "type": "H",
            "xyz": [
                -1.1963,
                0.9561,
                0.8911
            ]
        },
        {
            "type": "H",
            "xyz": [
                1.2242,
                0.9489,
                0.9188
            ]
        },
        {
            "type": "H",
            "xyz": [
                1.2105,
                1.0092,
                -0.854
            ]
        },
        {
            "type": "H",
            "xyz": [
                0.0291,
                -1.1038,
                -0.8296
            ]
        },
        {
            "type": "H",
            "xyz": [
                -2.46,
                -1.2431,
                -0.8498
            ]
        },
        {
            "type": "H",
            "xyz": [
                -2.4697,
                -1.1924,
                0.9358
            ]
        },
        {
            "type": "H",
            "xyz": [
                2.5076,
                -1.2058,
                0.8359
            ]
        },
        {
            "type": "H",
            "xyz": [
                2.5034,
                -1.1368,
                -0.9398
            ]
        }
    ],
    "id": 16164,
    "shapeM": [
        146.89,
        7.96,
        0.86,
        0.63,
        0.1,
        0.01,
        0.0,
        1.5,
        0.03,
        0.07,
        0.0,
        0.06,
        0.02,
        0.02
    ]
}

这是我的python脚本:

import numpy as np
import pandas as pd
import json
from itertools import groupby
from operator import itemgetter

with open('testdata.json') as f:
    data = json.load(f)

grouper = itemgetter("En")
total = 0
quantity = 0
for key, grp in groupby(sorted(data, key = grouper),grouper):
    total = float(''.join(map(str, [item["En"] for item in grp]))) #converting list to str to float
    quantity += 1
average = total/quantity
print(average,total,quantity)

0 个答案:

没有答案