Skip to content

Commit 98af91d

Browse files
authored
Merge pull request #298 from realpython/replace-string
How to Replace a String
2 parents 252ca6c + 19be581 commit 98af91d

File tree

7 files changed

+174
-0
lines changed

7 files changed

+174
-0
lines changed

replace-string-python/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# How to Replace a String in Python
2+
3+
These are the code examples from the Real Python tutorial [How to Replace a String in Python](https://realpython.com/replace-string-python/).
4+
5+
Additionally, there are a few extra code samples included here that didn't make it into the tutorial.

replace-string-python/bad_json.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import re
2+
import json
3+
4+
data = """{
5+
birthday = 1991
6+
name = Python
7+
creator = Guido van Rossum
8+
}"""
9+
10+
11+
def try_decode(data):
12+
print("Trying to decode...")
13+
try:
14+
output = json.loads(data)
15+
print("success")
16+
return output
17+
except json.decoder.JSONDecodeError as e:
18+
print("FAIL")
19+
print(e)
20+
21+
22+
try_decode(data)
23+
24+
25+
print(re.sub(pattern=r"(.+)(=)(.+)", repl=r'"\1":"\3"', string=data))
26+
try_decode(data)
27+
28+
29+
data = re.sub(pattern=r"\s+(.+)(\s+=\s+)(.+)", repl=r'"\1":"\3",', string=data)
30+
print(data)
31+
try_decode(data)
32+
33+
34+
# Remove last comma
35+
data = re.sub(pattern=r",$", repl="", string=data, flags=re.MULTILINE)
36+
print(data)
37+
data = try_decode(data)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import re
2+
import json
3+
4+
data = """{
5+
birthday = 1991
6+
name = Python
7+
creator = Guido van Rossum
8+
}"""
9+
10+
11+
def try_decode(data):
12+
print("Trying to decode...")
13+
try:
14+
output = json.loads(data)
15+
print("success")
16+
return output
17+
except json.decoder.JSONDecodeError as e:
18+
print("FAIL")
19+
print(e)
20+
21+
22+
def cleaner(match):
23+
return f'"{match.group(1).strip()}":"{match.group(3).strip()}",'
24+
25+
26+
data = re.sub(pattern=r"(.+)(=)(.+)", repl=cleaner, string=data)
27+
print(data)
28+
29+
# Remove last comma
30+
data = re.sub(pattern=r",(\s)+}", repl="}", string=data, flags=re.MULTILINE)
31+
print(data)
32+
33+
try_decode(data)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
transcript = """
2+
[support_tom] 2022-08-24T10:02:23+00:00 : What can I help you with?
3+
[johndoe] 2022-08-24T10:03:15+00:00 : I CAN'T CONNECT TO MY BLASTED ACCOUNT
4+
[support_tom] 2022-08-24T10:03:30+00:00 : Are you sure it's not your caps lock?
5+
[johndoe] 2022-08-24T10:04:03+00:00 : Blast! You're right!
6+
"""
7+
8+
print(transcript.replace("BLASTED", "😤"))
9+
10+
print(transcript.replace("BLASTED", "😤").replace("Blast", "😤"))
11+
12+
replacements = [
13+
("BLASTED", "😤"),
14+
("Blast", "😤"),
15+
("2022-08-24T", ""),
16+
("+00:00", ""),
17+
("[support_tom]", "Agent"),
18+
("[johndoe]", "Client"),
19+
]
20+
21+
for old, new in replacements:
22+
transcript = transcript.replace(old, new)
23+
24+
print(transcript)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from functools import reduce
2+
3+
transcript = """
4+
[support_tom] 2022-08-24T10:02:23+00:00 : What can I help you with?
5+
[johndoe] 2022-08-24T10:03:15+00:00 : I CAN'T CONNECT TO MY BLASTED ACCOUNT
6+
[support_tom] 2022-08-24T10:03:30+00:00 : Are you sure it's not your caps lock?
7+
[johndoe] 2022-08-24T10:04:03+00:00 : Blast! You're right!
8+
"""
9+
10+
replacements = [
11+
("BLASTED", "😤"),
12+
("Blast", "😤"),
13+
("2022-08-24T", ""),
14+
("+00:00", ""),
15+
("[support_tom]", "Tom"),
16+
("[johndoe]", "Client"),
17+
]
18+
19+
print(reduce(lambda acc, item: acc.replace(*item), replacements, transcript))
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import re
2+
3+
transcript = """
4+
[support_tom] 2022-08-24T10:02:23+00:00 : What can I help you with?
5+
[johndoe] 2022-08-24T10:03:15+00:00 : I CAN'T CONNECT TO MY BLASTED ACCOUNT
6+
[support_tom] 2022-08-24T10:03:30+00:00 : Are you sure it's not your caps lock?
7+
[johndoe] 2022-08-24T10:04:03+00:00 : Blast! You're right!
8+
"""
9+
10+
regex_replacements = [
11+
(r"blast\w*", "😤"),
12+
(r" [-T:\+\d]{25}", ""),
13+
(r"\[support\w*\]", "Agent"),
14+
(r"\[johndoe\]", "Client"),
15+
]
16+
17+
for regex_find, regex_replace in regex_replacements:
18+
transcript = re.sub(
19+
regex_find, regex_replace, transcript, flags=re.IGNORECASE
20+
)
21+
22+
print(transcript)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import re
2+
3+
transcript = """
4+
[support_tom] 2022-08-24T10:02:23+00:00 : What can I help you with?
5+
[johndoe] 2022-08-24T10:03:15+00:00 : I CAN'T CONNECT TO MY BLASTED ACCOUNT
6+
[support_tom] 2022-08-24T10:03:30+00:00 : Are you sure it's not your caps lock?
7+
[johndoe] 2022-08-24T10:04:03+00:00 : Blast! You're right!
8+
"""
9+
10+
message_pattern = r"(\[.+\]) ([-T:\+\d]{25}) : (.+)"
11+
12+
BAD_WORDS = ["blast", "dash", "beezlebub"]
13+
CLIENTS = ["johndoe", "janedoe"]
14+
15+
16+
def censor_bad_words(message):
17+
for word in BAD_WORDS:
18+
message = re.sub(rf"{word}\w*", "😤", message, flags=re.IGNORECASE)
19+
return message
20+
21+
22+
def censor_clients(user):
23+
for client in CLIENTS:
24+
user = re.sub(rf"\[{client}\]", "Client", user)
25+
return user
26+
27+
28+
def sanitize_message(match):
29+
user, _, message = match.groups()
30+
user = re.sub(r"\[.*support.*\]", "Agent", user)
31+
return f"{censor_clients(user)} : {censor_bad_words(message)}"
32+
33+
34+
print(re.sub(message_pattern, sanitize_message, transcript))

0 commit comments

Comments
 (0)