为了账号安全,请及时绑定邮箱和手机立即绑定

在字典python中删除\ n

在字典python中删除\ n

大话西游666 2022-07-05 18:51:42
我有一本字典,看起来像:{'values_changed': {'root': {'new_value': '{\n "quiz": {\n "sport": {\n "q1": {\n "question": "Which one is correct team name in NBA?",\n "options": [\n "New York Bulls",\n "Los Angeles Kings",\n "Golden Staxx000xxxte Warriros",\n "Huston Rocket"\n ],\n "answer": "Huston Rocket"\n }\n },\n "maths": {\n "q1": {\n "questxxx000xxcion": "5 + 7 = ?",\n "options": [\n "10",\n "11",\n "12",\n "13"\n ],\n "answer": "12"\n },\n "qx0000xx2": {\n "question": "12 - 8 = ?",\n "options": [\n "1",\n "2",\n "3",\n "4"\n ],\n "answer": "4"\n }\n }\n }\n}', 'old_value': '{\n "quiz": {\n "sport": {\n "q1": {\n "question": "Which one is correct team name in NBA?",\n "options": [\n "New York Bulls",\n "Los Angeles Kings",\n "Golden Staxxxxxte Warriros",\n "Huston Rocket"\n ],\n "answer": "Huston Rocket"\n }\n },\n "maths": {\n "q1": {\n "questxxxxxcion": "5 + 7 = ?",\n "options": [\n "10",\n "11",\n "12",\n "13"\n ],\n "answer": "12"\n },\n "qxxx2": {\n "question": "12 - 8 = ?",\n "options": [\n "1",\n "2",\n "3",\n "4"\n ],\n "answer": "4"\n }\n }\n }\n}', 'diff': '--- \n+++ \n@@ -6,7 +6,7 @@\n "options": [\n "New York Bulls",\n "Los Angeles Kings",\n- "Golden Staxxxxxte Warriros",\n+ "Golden Staxx000xxxte Warriros",\n "Huston Rocket"\n ],\n "answer": "Huston Rocket"\n@@ -14,7 +14,7 @@\n },\n "maths": {\n "q1": {\n- "questxxxxxcion": "5 + 7 = ?",\n+ "questxxx000xxcion": "5 + 7 = ?",\n "options": [\n "10",\n "11",\n@@ -23,7 +23,7 @@\n ],\n "answer": "12"\n },\n- "qxxx2": {\n+ "qx0000xx2": {\n "question": "12 - 8 = ?",\n "options": [\n "1",'}}}我需要从中删除 \n ;我试过 strip() 没有工作。如何从字典中删除 \n 以便我的字典如下所示。{'values_changed': {'root': {'new_value': '{ "quiz": {"sport": {  "q1": {  "question": "Which one is correct team name in NBA?",  "options":[  "New York Bulls",  "Los Angeles Kings",  "Golden Staxx000xxxte Warriros",  "Huston Rocket"  ],  "answer": "Huston Rocket"  }  },  "maths": {  "q1": {  "questxxx000xxcion": "5 + 7 = ?",  
查看完整描述

2 回答

?
海绵宝宝撒

TA贡献1809条经验 获得超8个赞

您可以使用递归来替换所有嵌套的字符串值,例如:


def remove_n(d):

    for k in d.keys():

        if type(d[k]) == str:

            d[k] = d[k].replace('\n', '')

        elif type(d[k]) == dict:

            remove_n(d[k])


d={'values_changed': {'root': {'new_value': '{\n "quiz": {\n "sport": {\n "q1": {\n "question": "Which one is correct team name in NBA?",\n "options": [\n "New York Bulls",\n "Los Angeles Kings",\n "Golden Staxx000xxxte Warriros",\n "Huston Rocket"\n ],\n "answer": "Huston Rocket"\n }\n },\n "maths": {\n "q1": {\n "questxxx000xxcion": "5 + 7 = ?",\n "options": [\n "10",\n "11",\n "12",\n "13"\n ],\n "answer": "12"\n },\n "qx0000xx2": {\n "question": "12 - 8 = ?",\n "options": [\n "1",\n "2",\n "3",\n "4"\n ],\n "answer": "4"\n }\n }\n }\n}', 'old_value': '{\n "quiz": {\n "sport": {\n "q1": {\n "question": "Which one is correct team name in NBA?",\n "options": [\n "New York Bulls",\n "Los Angeles Kings",\n "Golden Staxxxxxte Warriros",\n "Huston Rocket"\n ],\n "answer": "Huston Rocket"\n }\n },\n "maths": {\n "q1": {\n "questxxxxxcion": "5 + 7 = ?",\n "options": [\n "10",\n "11",\n "12",\n "13"\n ],\n "answer": "12"\n },\n "qxxx2": {\n "question": "12 - 8 = ?",\n "options": [\n "1",\n "2",\n "3",\n "4"\n ],\n "answer": "4"\n }\n }\n }\n}', 'diff': '--- \n+++ \n@@ -6,7 +6,7 @@\n "options": [\n "New York Bulls",\n "Los Angeles Kings",\n- "Golden Staxxxxxte Warriros",\n+ "Golden Staxx000xxxte Warriros",\n "Huston Rocket"\n ],\n "answer": "Huston Rocket"\n@@ -14,7 +14,7 @@\n },\n "maths": {\n "q1": {\n- "questxxxxxcion": "5 + 7 = ?",\n+ "questxxx000xxcion": "5 + 7 = ?",\n "options": [\n "10",\n "11",\n@@ -23,7 +23,7 @@\n ],\n "answer": "12"\n },\n- "qxxx2": {\n+ "qx0000xx2": {\n "question": "12 - 8 = ?",\n "options": [\n "1",'}}}


remove_n(d)


查看完整回答
反对 回复 2022-07-05
?
慕标5832272

TA贡献1966条经验 获得超4个赞

尝试这个:

d = {'values_changed': {'root': {'new_value': '{\n "quiz": {\n "sport": {\n "q1": {\n "question": "Which one is correct team name in NBA?",\n "options": [\n "New York Bulls",\n "Los Angeles Kings",\n "Golden Staxx000xxxte Warriros",\n "Huston Rocket"\n ],\n "answer": "Huston Rocket"\n }\n },\n "maths": {\n "q1": {\n "questxxx000xxcion": "5 + 7 = ?",\n "options": [\n "10",\n "11",\n "12",\n "13"\n ],\n "answer": "12"\n },\n "qx0000xx2": {\n "question": "12 - 8 = ?",\n "options": [\n "1",\n "2",\n "3",\n "4"\n ],\n "answer": "4"\n }\n }\n }\n}', 'old_value': '{\n "quiz": {\n "sport": {\n "q1": {\n "question": "Which one is correct team name in NBA?",\n "options": [\n "New York Bulls",\n "Los Angeles Kings",\n "Golden Staxxxxxte Warriros",\n "Huston Rocket"\n ],\n "answer": "Huston Rocket"\n }\n },\n "maths": {\n "q1": {\n "questxxxxxcion": "5 + 7 = ?",\n "options": [\n "10",\n "11",\n "12",\n "13"\n ],\n "answer": "12"\n },\n "qxxx2": {\n "question": "12 - 8 = ?",\n "options": [\n "1",\n "2",\n "3",\n "4"\n ],\n "answer": "4"\n }\n }\n }\n}', 'diff': '--- \n+++ \n@@ -6,7 +6,7 @@\n "options": [\n "New York Bulls",\n "Los Angeles Kings",\n- "Golden Staxxxxxte Warriros",\n+ "Golden Staxx000xxxte Warriros",\n "Huston Rocket"\n ],\n "answer": "Huston Rocket"\n@@ -14,7 +14,7 @@\n },\n "maths": {\n "q1": {\n- "questxxxxxcion": "5 + 7 = ?",\n+ "questxxx000xxcion": "5 + 7 = ?",\n "options": [\n "10",\n "11",\n@@ -23,7 +23,7 @@\n ],\n "answer": "12"\n },\n- "qxxx2": {\n+ "qx0000xx2": {\n "question": "12 - 8 = ?",\n "options": [\n "1",'}}}

该值是一个字符串,如果您希望将其转换为 dict/json,您可以:

d["values_changed"]["root"]["new_value"] = simplejson.loads(d["values_changed"]["root"]["new_value"])

你可以做类似的事情old_value


查看完整回答
反对 回复 2022-07-05
  • 2 回答
  • 0 关注
  • 250 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信