将单引号dict转换为有效的json

时间:2015-06-03 16:31:30

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

2 个答案:

答案 0 :(得分:2)

然而,似乎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}})

答案 1 :(得分:0)

找到答案。这是有效的功能:

<xsl:transform version="1.0"
  xmlns:xsl=""
  xmlns:se="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:urn="urn:cidx:names:specification:ces:schema:all:5:0"
  exclude-result-prefixes="se urn"
>

  <!-- if you positively must be this specific -->
  <xsl:variable name="docId" select="/se:Envelope/se:Body/*/ThisDocumentIdentifier" />

  <!-- simpler/nicer is this -->
  <xsl:variable name="docId_nicer" select="//ThisDocumentIdentifier" />
</xsl:transform>