python - 在 python 中使用 json 将数字格式化为 float 或 int

标签 python json csv

我有一个包含数据的 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

import json
import csv

csvfile_ind = open("test.csv",'r')

reader_ind = csv.DictReader(csvfile_ind)
json_file_ind = open("test_json.json", 'w')
for row in reader_ind:
    json_file_ind.write(json.dumps(row,sort_keys=False, indent=4, separators=(',', ': ')))

我当前的输出是 -

        [
          {
            "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。

最佳答案

不要将每一行编写为对 json.dumps() 的单独调用。将所有行收集到一个列表中,然后一次性全部转储。

要将字符串字段转换为整数,请对 dict 中的这些条目调用 int()

import json
import csv

with csvfile_ind = open("test.csv",'r'):
    reader_ind = csv.DictReader(csvfile_ind)
    rows = []
    for row in reader_ind:
        row["RRC_attempts"] = int(row["RRC_attempts"])
        row["rrc_succ_rate"] = int(row["rrc_succ_rate"])
        rows.append(row)

with json_file_ind = open("test_json.json", 'w'):
    json.dump(rows, json_file_ind, sort_keys=False, indent=4, separators=(',', ': '))

关于python - 在 python 中使用 json 将数字格式化为 float 或 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48233433/

相关文章:

javascript - 保护本地主机 JavaScript ajax 请求

python - 如何在 python 中获取打开文件的 win32 句柄?

python - 什么是 numpy.core._multiarray_umath.implement_array_function 以及为什么要花费大量时间?

python - 在 Numpy/Scipy 中切片数组

Java Spring : Web Service REST producing double backslash at JSON

java - 自定义和不同的 json 日期格式

php - 从 PDO 查询返回一个包含列名的二维数组

r - 将日期时间字符串转换为 R 中带时间的日期

csv - 如何更新 csv::ByteRecord 中的字段?

json - 配置单元 : How to explode a JSON column with an array, 并嵌入到 CSV 文件中?