我将此json输出存储在dict中。我想从中删除“你”以及关键字“social_tone”和“Social Tone”。下面是我的字典。
{u'document_tone': {u'tone_categories': [{u'category_id': u'emotion_tone', u'tones': [{u'tone_name': u'Anger', u'score': 0.573181, u'tone_id': u'anger'})
答案 0 :(得分:5)
这不是JSON,这是Python字典的字符串表示形式。你可能正在做类似str(my_dict)
的事情。
要将字典编码为正确的JSON,请使用json.dumps
。
import json
my_dict = {u'document_tone': {u'tone_categories': []}}
my_json = json.dumps(my_dict)
这将产生预期的输出:
{"document_tone": {"tone_categories": []}}
答案 1 :(得分:1)
那不是json,它是python dict,你可以使用json.dumps
将dict转换为json字符串
import json
d = {u'document_tone': {u'tone_categories': [{u'category_id': u'emotion_tone', u'tones': [{u'tone_name': u'Anger', u'score': 0.573181, u'tone_id': u'anger'}, {u'tone_name': u'Disgust', u'score': 0.07296, u'tone_id': u'disgust'}, {u'tone_name': u'Fear', u'score': 0.129168, u'tone_id': u'fear'}, {u'tone_name': u'Joy', u'score': 0.058345, u'tone_id': u'joy'}, {u'tone_name': u'Sadness', u'score': 0.395484, u'tone_id': u'sadness'}], u'category_name': u'Emotion Tone'}, {u'category_id': u'language_tone', u'tones': [{u'tone_name': u'Analytical', u'score': 0.9762, u'tone_id': u'analytical'}, {u'tone_name': u'Confident', u'score': 0.0, u'tone_id': u'confident'}, {u'tone_name': u'Tentative', u'score': 0.733641, u'tone_id': u'tentative'}], u'category_name': u'Language Tone'}, {u'category_id': u'social_tone', u'tones': [{u'tone_name': u'Openness', u'score': 0.495667, u'tone_id': u'openness_big5'}, {u'tone_name': u'Conscientiousness', u'score': 0.194302, u'tone_id': u'conscientiousness_big5'}, {u'tone_name': u'Extraversion', u'score': 0.705757, u'tone_id': u'extraversion_big5'}, {u'tone_name': u'Agreeableness', u'score': 0.189904, u'tone_id': u'agreeableness_big5'}, {u'tone_name': u'Emotional Range', u'score': 0.850804, u'tone_id': u'emotional_range_big5'}], u'category_name': u'Social Tone'}]}, u'sentences_tone': [{u'input_to': 129, u'text': u'The argument states that as the physical injury occurring on the job increases, the wages paid to employees should also increase.', u'sentence_id': 0, u'input_from': 0, u'tone_categories': [{u'category_id': u'emotion_tone', u'tones': [{u'tone_name': u'Anger', u'score': 0.261539, u'tone_id': u'anger'}, {u'tone_name': u'Disgust', u'score': 0.209928, u'tone_id': u'disgust'}, {u'tone_name': u'Fear', u'score': 0.114434, u'tone_id': u'fear'}, {u'tone_name': u'Joy', u'score': 0.054893, u'tone_id': u'joy'}, {u'tone_name': u'Sadness', u'score': 0.360565, u'tone_id': u'sadness'}], u'category_name': u'Emotion Tone'}, {u'category_id': u'language_tone', u'tones': [{u'tone_name': u'Analytical', u'score': 0.398794, u'tone_id': u'analytical'}, {u'tone_name': u'Confident', u'score': 0.0, u'tone_id': u'confident'}, {u'tone_name': u'Tentative', u'score': 0.0, u'tone_id': u'tentative'}], u'category_name': u'Language Tone'}, {u'category_id': u'social_tone', u'tones': [{u'tone_name': u'Openness', u'score': 0.341757, u'tone_id': u'openness_big5'}, {u'tone_name': u'Conscientiousness', u'score': 0.53574, u'tone_id': u'conscientiousness_big5'}, {u'tone_name': u'Extraversion', u'score': 0.617458, u'tone_id': u'extraversion_big5'}, {u'tone_name': u'Agreeableness', u'score': 0.512805, u'tone_id': u'agreeableness_big5'}, {u'tone_name': u'Emotional Range', u'score': 0.944333, u'tone_id': u'emotional_range_big5'}], u'category_name': u'Social Tone'}]}, {u'input_to': 249, u'text': u'Hence it makes sense for employers to make the workplace safer, thereby reducing their payroll expenses and save money.', u'sentence_id': 1, u'input_from': 130, u'tone_categories': [{u'category_id': u'emotion_tone', u'tones': [{u'tone_name': u'Anger', u'score': 0.404165, u'tone_id': u'anger'}, {u'tone_name': u'Disgust', u'score': 0.060763, u'tone_id': u'disgust'}, {u'tone_name': u'Fear', u'score': 0.162228, u'tone_id': u'fear'}, {u'tone_name': u'Joy', u'score': 0.216902, u'tone_id': u'joy'}, {u'tone_name': u'Sadness', u'score': 0.149858, u'tone_id': u'sadness'}], u'category_name': u'Emotion Tone'}, {u'category_id': u'language_tone', u'tones': [{u'tone_name': u'Analytical', u'score': 0.952597, u'tone_id': u'analytical'}, {u'tone_name': u'Confident', u'score': 0.0, u'tone_id': u'confident'}, {u'tone_name': u'Tentative', u'score': 0.0, u'tone_id': u'tentative'}], u'category_name': u'Language Tone'}, {u'category_id': u'social_tone', u'tones': [{u'tone_name': u'Openness', u'score': 0.186754, u'tone_id': u'openness_big5'}, {u'tone_name': u'Conscientiousness', u'score': 0.304324, u'tone_id': u'conscientiousness_big5'}, {u'tone_name': u'Extraversion', u'score': 0.705324, u'tone_id': u'extraversion_big5'}, {u'tone_name': u'Agreeableness', u'score': 0.41322, u'tone_id': u'agreeableness_big5'}, {u'tone_name': u'Emotional Range', u'score': 0.890612, u'tone_id': u'emotional_range_big5'}], u'category_name': u'Social Tone'}]}, {u'input_to': 312, u'text': u'Stated in this way the argument is incomplete and unjustified.', u'sentence_id': 2, u'input_from': 250, u'tone_categories': [{u'category_id': u'emotion_tone', u'tones': [{u'tone_name': u'Anger', u'score': 0.489648, u'tone_id': u'anger'}, {u'tone_name': u'Disgust', u'score': 0.060676, u'tone_id': u'disgust'}, {u'tone_name': u'Fear', u'score': 0.043002, u'tone_id': u'fear'}, {u'tone_name': u'Joy', u'score': 0.066779, u'tone_id': u'joy'}, {u'tone_name': u'Sadness', u'score': 0.55297, u'tone_id': u'sadness'}], u'category_name': u'Emotion Tone'}, {u'category_id': u'language_tone', u'tones': [{u'tone_name': u'Analytical', u'score': 0.896021, u'tone_id': u'analytical'}, {u'tone_name': u'Confident', u'score': 0.0, u'tone_id': u'confident'}, {u'tone_name': u'Tentative', u'score': 0.497569, u'tone_id': u'tentative'}], u'category_name': u'Language Tone'}, {u'category_id': u'social_tone', u'tones': [{u'tone_name': u'Openness', u'score': 0.844606, u'tone_id': u'openness_big5'}, {u'tone_name': u'Conscientiousness', u'score': 0.258585, u'tone_id': u'conscientiousness_big5'}, {u'tone_name': u'Extraversion', u'score': 0.608366, u'tone_id': u'extraversion_big5'}, {u'tone_name': u'Agreeableness', u'score': 0.077368, u'tone_id': u'agreeableness_big5'}, {u'tone_name': u'Emotional Range', u'score': 0.209902, u'tone_id': u'emotional_range_big5'}], u'category_name': u'Social Tone'}]}, {u'input_to': 366, u'text': u'The argument is confusing causality with correlation.', u'sentence_id': 3, u'input_from': 313, u'tone_categories': [{u'category_id': u'emotion_tone', u'tones': [{u'tone_name': u'Anger', u'score': 0.46968, u'tone_id': u'anger'}, {u'tone_name': u'Disgust', u'score': 0.030388, u'tone_id': u'disgust'}, {u'tone_name': u'Fear', u'score': 0.161173, u'tone_id': u'fear'}, {u'tone_name': u'Joy', u'score': 0.065839, u'tone_id': u'joy'}, {u'tone_name': u'Sadness', u'score': 0.449657, u'tone_id': u'sadness'}], u'category_name': u'Emotion Tone'}, {u'category_id': u'language_tone', u'tones': [{u'tone_name': u'Analytical', u'score': 0.974578, u'tone_id': u'analytical'}, {u'tone_name': u'Confident', u'score': 0.0, u'tone_id': u'confident'}, {u'tone_name': u'Tentative', u'score': 0.91961, u'tone_id': u'tentative'}], u'category_name': u'Language Tone'}, {u'category_id': u'social_tone', u'tones': [{u'tone_name': u'Openness', u'score': 0.914923, u'tone_id': u'openness_big5'}, {u'tone_name': u'Conscientiousness', u'score': 0.099889, u'tone_id': u'conscientiousness_big5'}, {u'tone_name': u'Extraversion', u'score': 0.769632, u'tone_id': u'extraversion_big5'}, {u'tone_name': u'Agreeableness', u'score': 0.312134, u'tone_id': u'agreeableness_big5'}, {u'tone_name': u'Emotional Range', u'score': 0.062347, u'tone_id': u'emotional_range_big5'}], u'category_name': u'Social Tone'}]}]}
json.dumps(d)
答案 2 :(得分:-3)
这是我对你的问题的答案
my_str="your massive string here"
data=my_str.replace("u'","").replace("social_tone","").replace("Social Tone","")
print data