python - 将单引号字典转换为有效的 json

标签 python json

这可能看起来很简单,但它让我困惑了一天,所以我向你们求助。

我有一个有效的 Python 字典:

{'numeric_int2': {'(113.7, 211.4]': 3,
  '(15.023, 113.7]': 4,
  '(211.4, 309.1]': 5,
  '(309.1, 406.8]': 4,
  '(406.8, 504.5]': 5,
  '(504.5, 602.2]': 7,
  '(602.2, 699.9]': 4,
  '(699.9, 797.6]': 5,
  '(797.6, 895.3]': 4,
  '(895.3, 993]': 6}}

我想将其转换为有效的 JSON,这需要删除单引号。期望的最终结果是:

{"numeric_int2": {"(113.7, 211.4]": 3,
  "(15.023, 113.7]": 4,
  "(211.4, 309.1]": 5,
  "(309.1, 406.8]": 4,
  "(406.8, 504.5]": 5,
  "(504.5, 602.2]": 7,
  "(602.2, 699.9]": 4,
  "(699.9, 797.6]": 5,
  "(797.6, 895.3]": 4,
  "(895.3, 993]": 6}}

我已经尝试了 json.dumps() 或其他任何我能想到的方法。我怎样才能做到这一点?如果速度快的话就加分。

我应该补充一点,当我尝试在字典上使用 json.dumps() 时,出现错误:

TypeError: 1 is not JSON serializable

这是我的完整代码:

In [17]:

import pandas as pd
import numpy as np
import itertools
import simplejson
​
raw_data = {
    'numeric_float1': list([np.random.random() for _ in range(0, 47)]+[np.nan]),
    'numeric_float2': list([np.random.random() for _ in range(0, 47)]+[np.nan]),
    'numeric_float3': list([np.random.random() for _ in range(0, 47)]+[np.nan]),
    }
​
df = pd.DataFrame(raw_data)
​
df_labels = [
    'category1:category',
    'numeric_float1:numeric',
    'numeric_float2:numeric',
    'numeric_float3:numeric'
    ]
​
columns = list(zip([w.split(':')[0] for w in df_labels],[w.split(':')[1] for w in df_labels]))
In [18]:

def count_by_value(df,columns):
    numeric = [c[0] for c in columns if c[1] == 'numeric']

    output = {}

    for column in df[numeric]:
        output[column] = pd.cut(df[column],10).value_counts().to_dict()

    output = simplejson.dumps(output)

    return output
In [19]:

# Test the function
count_by_value(df,columns)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-19-02e2e6cb949b> in <module>()
      1 # Test the function
----> 2 count_by_value(df,columns)

<ipython-input-18-c2d882f5652d> in count_by_value(df, columns)
      9         output[column] = pd.cut(df[column],10).value_counts().to_dict()
     10 
---> 11     output = simplejson.dumps(output)
     12 
     13     return output

/Users/antonnobla/anaconda/lib/python3.4/site-packages/simplejson/__init__.py in dumps(obj, skipkeys, ensure_ascii, check_circular, allow_nan, cls, indent, separators, encoding, default, use_decimal, namedtuple_as_object, tuple_as_array, bigint_as_string, sort_keys, item_sort_key, for_json, ignore_nan, int_as_string_bitcount, **kw)
    368         and not kw
    369     ):
--> 370         return _default_encoder.encode(obj)
    371     if cls is None:
    372         cls = JSONEncoder

/Users/antonnobla/anaconda/lib/python3.4/site-packages/simplejson/encoder.py in encode(self, o)
    268         # exceptions aren't as detailed.  The list call should be roughly
    269         # equivalent to the PySequence_Fast that ''.join() would do.
--> 270         chunks = self.iterencode(o, _one_shot=True)
    271         if not isinstance(chunks, (list, tuple)):
    272             chunks = list(chunks)

/Users/antonnobla/anaconda/lib/python3.4/site-packages/simplejson/encoder.py in iterencode(self, o, _one_shot)
    350                 Decimal=decimal.Decimal)
    351         try:
--> 352             return _iterencode(o, 0)
    353         finally:
    354             key_memo.clear()

/Users/antonnobla/anaconda/lib/python3.4/site-packages/simplejson/encoder.py in default(self, o)
    245 
    246         """
--> 247         raise TypeError(repr(o) + " is not JSON serializable")
    248 
    249     def encode(self, o):

TypeError: 4 is not JSON serializable

最佳答案

看来 simplejson 和 json 都按我的预期工作,但是 simplejson 比 json 快(快很多),而且它似乎可以很好地处理您的数据

import simplejson,json
print simplejson.dumps({'numeric_int2': {'(113.7, 211.4]': 3,
'(15.023, 113.7]': 4,
'(211.4, 309.1]': 5,
'(309.1, 406.8]': 4,
'(406.8, 504.5]': 5,
'(504.5, 602.2]': 7,
'(602.2, 699.9]': 4,
'(699.9, 797.6]': 5,
'(797.6, 895.3]': 4,
'(895.3, 993]': 6}})

print json.dumps({'numeric_int2': {'(113.7, 211.4]': 3,
'(15.023, 113.7]': 4,
'(211.4, 309.1]': 5,
'(309.1, 406.8]': 4,
'(406.8, 504.5]': 5,
'(504.5, 602.2]': 7,
'(602.2, 699.9]': 4,
'(699.9, 797.6]': 5,
'(797.6, 895.3]': 4,
'(895.3, 993]': 6}})

关于python - 将单引号字典转换为有效的 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30625787/

相关文章:

PHP pdo foreach

android - 尝试修复 NetworkOnMainThreadException 但出现 Toast 错误

javascript - 如何用 Javascript 捕获来自另一个网站的脚本的响应(它返回纯文本)?

json - 如何从 BigQuery 中的平面表创建嵌套的 JSON 格式表?

用于处理linux的audit.log的Python库?

python - 在 Python 中模拟目录结构

python - Heroku 上的基本 Django 静态文件部署

python - 我想要绘制一些 Facebook Insights 报告,但日期时间与我的格式不匹配

python - 值错误 : Number of features of the model must match the input

java - 我无法从 JSON 数组获取图像字节,有人可以帮助我吗?