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)
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
添加回答
举报