2015-06-03 3 views
1

Это может показаться простым, но это захлестнуло меня на день, поэтому я обращаюсь к вам.Преобразование одиночной кавычки в действительный 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

json.dumps выглядит как правильный выход для меня ... –

+0

@JoranBeasley прикомандирован. Голосование закрывается. – IanAuld

+0

json.dumps терпит неудачу, говоря, что TypeError: 1 не является сериализуемым JSON – Anton

ответ

0

оказывается как 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}}) 
+0

jsdon.dumps() работает для меня тоже. –

0

Найден ответ. Вот функция, которая работает:

# Count the frequency of each value 
def count_by_value(df,columns): 
    # Selects appropriate columns for the action 
    numeric = [c[0] for c in columns if c[1] == 'numeric'] 

    # Returns 0 if none of the appropriate columns exists 
    if len(numeric) == 0: 
     return 0 

    output = pd.DataFrame() 

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

    output = output.to_json() 

    # output results 
    return output 
Смежные вопросы