我有一个CSV文件,里面有数据-
Time,site_name,cell_name,RRC_attempts,rrc_succ_rate
2018-01-12T08:37:00-06:00,910536_ARPIN,910536-24,1,100.0
2018-01-12T08:37:00-06:00,910536_ARPIN,910536-34,0,0.0
2018-01-12T08:37:00-06:00,910536_ARPIN,910536-14,5,100.0
我使用python中的json模块将csv转换为json
^{pr2}$我现在的输出是-
[
{
"Time": "2018-01-12T08:37:00-06:00",
"site_name": "910536_ARPIN",
"cell_name": "910536-24",
"RRC_attempts": "1",
"rrc_succ_rate": "100.0"
},
{
"Time": "2018-01-12T08:37:00-06:00",
"site_name": "910536_ARPIN",
"cell_name": "910536-34",
"RRC_attempts": "0",
"rrc_succ_rate": "0.0"
},
{
"Time": "2018-01-12T08:37:00-06:00",
"site_name": "910536_ARPIN",
"cell_name": "910536-14",
"RRC_attempts": "5",
"rrc_succ_rate": "100.0"
}
]
我想要的输出是-
[
{
"Time": "2018-01-12T08:37:00-06:00",
"site_name": "910536_ARPIN",
"cell_name": "910536-24",
"RRC_attempts": 1,
"rrc_succ_rate": 100
},
{
"Time": "2018-01-12T08:37:00-06:00",
"site_name": "910536_ARPIN",
"cell_name": "910536-34",
"RRC_attempts": 0,
"rrc_succ_rate": 0
},
{
"Time": "2018-01-12T08:37:00-06:00",
"site_name": "910536_ARPIN",
"cell_name": "910536-14",
"RRC_attempts": 5,
"rrc_succ_rate": 100
}
]
如何告诉json将数字解析为int或float而不是字符串?请告知。 注意-在编写CSV文件时,我使用int()或float()将值显式转换为int或float。在
这里有一种方法,您不必事先知道哪些值是数值:
在创建字典Parse}时,自己解析csv,输出到json:
int()
和{Output:(由
^{pr2}$json_file_ind
-调用创建的文件内容)不要将每一行作为对
json.dumps()
的单独调用编写。将所有行收集到一个列表中,并一次性将其全部转储。在要将字符串字段转换为整数,请对
dict
中的那些条目调用int()
。在相关问题 更多 >
编程相关推荐