Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我将此json输出存储在dict中。我想删除;u";从它和钥匙;社交“语气”;及;社交腔调;。下面是我的字典_Python_Json_Dictionary - Fatal编程技术网

Python 我将此json输出存储在dict中。我想删除;u";从它和钥匙;社交“语气”;及;社交腔调;。下面是我的字典

Python 我将此json输出存储在dict中。我想删除;u";从它和钥匙;社交“语气”;及;社交腔调;。下面是我的字典,python,json,dictionary,Python,Json,Dictionary,我将这个json输出存储在dict中。我想从中删除“u”,以及键“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'}) 这不是JSON,而是Python字典的字符串

我将这个json输出存储在dict中。我想从中删除“u”,以及键“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'})

这不是JSON,而是Python字典的字符串表示形式。您可能正在执行类似于
str(my_dict)
的操作

要将字典编码为正确的JSON,请使用

这将产生预期的输出:

{"document_tone": {"tone_categories": []}}

这不是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)

这是我对你问题的回答

my_str="your massive string here"

data=my_str.replace("u'","").replace("social_tone","").replace("Social Tone","")

print data

这将帮助你更好地理解。
u
并不是字典中实际包含的内容,它只是unicode字符串的表示方式。您必须提供更多的上下文,才能获得关于如何删除键“social\u tone”的好答案。你到底想删除什么?dict是否总是具有相同的结构?我建议问另外一个问题,关于删除同时包含预期输入和预期输出的“社交语气”。我这样做了,并且能够从中删除“u”。但是,当我进入一个循环时,我不断得到错误类型错误:字符串索引必须是整数,而不是str我的循环是:对于json中的i_final2['document_tone']['tone_categories']:print(i['category_name'])print(“-”*len(i['categority_name']))对于i['tones']:print(j['tone_name'].ljust(20),(str(round(j['score']*10,1))).rjust(10))print()print()JSON是数据结构的字符串表示形式。所以json.dumps只返回一个简单的字符串,你不能像那样迭代。@YashBhagwatkar请不要编辑一篇文章,用一个完全不同的问题替换它的全部内容。你可以使用
json.loads
像my_json=json.loads(json.dumps(my_dict))…然后当它循环@Yash Bhagwatkar时,你会得到结果
my_str="your massive string here"

data=my_str.replace("u'","").replace("social_tone","").replace("Social Tone","")

print data