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

时间:2018-01-12 20:18:45

标签: 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。

4 个答案:

答案 0 :(得分:1)

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

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

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=(',', ': '))

答案 1 :(得分:0)

自己解析csv,在需要时创建字典解析int()float(),输出到json:

import json 

with open("test.csv",'r') as f:
    # read lines, strip newlines, split at ,
    lines = [ x.strip('\n').split(',') for x in f.readlines()]    


listDic = []
for lineIndex in range(1,len(lines)):
    row = lines[lineIndex]     # get data row
    row[3] = int(row[3])       # convert data
    row[4] = float(row[4])     # convert data

    # zip to tuples of (key,value) and append to result list of dicts 
    listDic.append( dict( zip(lines[0],row)))  


with open("test_json.json", 'w') as json_file_ind:
    for row in listDic:
        json_file_ind.write(json.dumps(row,sort_keys=False, 
                            indent=4, separators=(',', ': ')))

输出:(由json_file_ind - 调用创建的文件内容)

{
    "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.07
}{
    "Time": "2018-01-12T08:37:00-06:00",
    "site_name": "910536_ARPIN",
    "cell_name": "910536-14",
    "RRC_attempts": 5,
    "rrc_succ_rate": 100.0
}

答案 2 :(得分:0)

以下是一种您不知道先验哪些值为数字的方法:

import json
import csv

def numerify(row):
    for k, v in list(row.items()):
        try:
            row[k] = float(v)
            row[k] = int(v)
        except ValueError:
            pass

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:
    numerify(row)
    json_file_ind.write(json.dumps(row,sort_keys=False, indent=4, separators=(',', ': ')))

答案 3 :(得分:0)

如果您希望将ints设置为int并将floats设置为float,则可以如下调整Robs代码:

def numerify(row):
for k, v in list(row.items()):
    try:
        row[k] = int(v)
    except ValueError:
        try:
            row[k] = float(v)
        except ValueError:
            pass
相关问题