-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_task2.py
More file actions
50 lines (46 loc) · 1.64 KB
/
python_task2.py
File metadata and controls
50 lines (46 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""
** Requirements:
- Give a UNKNOWN NESTED LEVEL dictionary. We don't know the dictionary's shape or any dictionary's metadata
- Write a function to remove all the newline characters \n and the escaped newline characters \\n on all string value in dict
- All other value's data type is kept normally
- All two or more whitespace characters side by side should be replaced by single whitespace
"""
# For example:
# This dictionary below is messy and we need to remove all newline characters
dict_sample = {
"texts": [
{
"title": "The \n\n spiderman",
"content": "This is the content\n\n. After new line\n. Final new line\n",
},
{"rate": 4.5, "content": "Content has\n\n"},
"This is the text with newline \\n\\n",
{
"random_array": ["Hello \n \\n World", "No new line"],
"string": "This is a random string\n\n",
},
],
"description": "This description has\n\n",
"user_001": {"name": "John \n\n Smith"},
}
# The correct output should be
processed_dict = {
"texts": [
{
"title": "The spiderman",
"content": "This is the content. After new line. Final new line",
},
{"rate": 4.5, "content": "Content has"},
"This is the text with newline",
{
"random_array": ["Hello World", "No new line"],
"string": "This is a random string",
},
],
"description": "This description has",
"user_001": {"name": "John Smith"},
}
def extracted_dict_string(raw_dict: dict):
new_dict = raw_dict
# TODO: Write your code to change new_dict here
return new_dict