将特定键的值格式化为数字/整数/浮点数时,将CSV转换为嵌套JSON

阿留

我正在尝试将CS​​V文件转换为嵌套JSON,这是我的CSV,其中第一行为列。

CLID,District, attribute,value
C001,Tebuslik, Name,Philip
C001,Tebuslik,Age,34
C002,Hontenlo,Name,Jane
C002,Hontenlo,Age,23

我想要的输出是一个嵌套的json,其中Age的值是数字而不是字符串。

[
    {
        "CLID": "C001",
        "District": "Tebuslik",
        "attributes": [
            {
                "attribute": "Name",
                "value": "Philip"
            },
            {
                "attribute": "Age",
                "value": 34
            }
        ]
    },
    {
        "CLID": "C002",
        "District": "Hontenlo",
        "attributes": [
            {
                "attribute": "Name",
                "value": "Jane"
            },
            {
                "attribute": "Age",
                "value": 23
            }
        ]
    }
]

在我的CSV中,所有键共享同一列(Attribute),并且值可以是字符串或数字格式,具体取决于属性。

这是我的python脚本,可以正常工作:

from csv import DictReader
from itertools import groupby
from pprint import pprint
import json

with open('teis.csv') as csvfile:
    r = DictReader(csvfile, skipinitialspace=True)
    data = [dict(d) for d in r]

    groups = []
    uniquekeys = []

    for k, g in groupby(data, lambda r: (r['CLID'], r['District'])):
        groups.append({
            "CLID": k[0],
            "District": k[1],
            "attributes": [{k:v for k, v in d.items() if k not in ['CLID','District']} for d in list(g)]
        })
        uniquekeys.append(k)

print(json.dumps(groups, indent = 4) + '\n}')

但是,下面是我用引用的数字年龄值得到的输出;

[
    {
        "CLID": "C001",
        "District": "Tebuslik",
        "attributes": [
            {
                "attribute": "Name",
                "value": "Philip"
            },
            {
                "attribute": "Age",
                "value": "34"
            }
        ]
    },
    {
        "CLID": "C002",
        "District": "Hontenlo",
        "attributes": [
            {
                "attribute": "Name",
                "value": "Jane"
            },
            {
                "attribute": "Age",
                "value": "23"
            }
        ]
    }
]
拉克什

使用str.isdigit检查字符串,然后使用int

例如:

from csv import DictReader
from itertools import groupby
from pprint import pprint
import json

with open(filename) as csvfile:
    r = DictReader(csvfile, skipinitialspace=True)
    data = [dict(d) for d in r]

    groups = []
    uniquekeys = []

    for k, g in groupby(data, lambda r: (r['CLID'], r['District'])):
        groups.append({
            "CLID": k[0],
            "District": k[1],
            "attributes": [{k:int(v) if v.isdigit() else v for k, v in d.items() if k not in ['CLID','District']} for d in list(g)]  #Update
        })
        uniquekeys.append(k)

print(json.dumps(groups, indent = 4) + '\n}')

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Java将浮点数转换为整数

将整数转换为浮点数

将浮点数转换为hh:mm格式

朱莉娅:将数字字符串转换为浮点数或整数

将浮点数转换为整数时,尾随数字从何而来?

当.toFixed(2)在小数点后给出零时,如何将浮点数格式化为整数?

如何将熊猫系列中的数字转换为整数或浮点数?

将浮点数转换为具有数字和nan的列的整数

将字符串转换为数字(根据输入的变量为浮点数或整数)

将浮点数与数组中的值进行比较时,“ TypeError:只能将整数标量数组转换为标量索引”

如何将浮点数转换为整数?

python-将字符串转换为数字(浮点数或整数)的问题

将整数列表转换为浮点数

输入type = number:当点后有3位数字时,Firefox将浮点数转换为整数

将数字10转换为0.10浮点数

将浮点数转换为整数

将浮点数转换为十六进制格式

python数据帧将整数转换为浮点数

从 Python 中的 csv 文件读取时将字符串转换为整数/浮点数

尝试添加整数时,SoapUI 属性传输将值转换为浮点数

将浮点数转换为整数以获得没有点和零的数字

将整数转换为浮点数

无法将浮点数转换为整数

如何将整数转换为浮点数?

如何将整数或浮点数转换为 S16.16 定点格式

将javascript数字转换为浮点数(单精度IEEE-754)并接收整数位

Python:如果需要,将浮点数格式化为最小浮点精度

如何将值(以时间格式)转换为数字(浮点数)

仅将列的浮点数转换为整数