Skip to content

Commit 6afb7d2

Browse files
committed
Add code for string join tutorial
1 parent 2b76a5c commit 6afb7d2

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

python-join-strings/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# How to Join a String in Python
2+
3+
This folder contains code associated with the Real Python tutorial on [How to Join a String in Python](https://realpython.com/python-join-string/).
4+
5+
## About the Author
6+
7+
Martin Breuss - Email: [email protected]
8+
9+
## License
10+
11+
Distributed under the MIT license. See `LICENSE` for more information.

python-join-strings/event_log.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"2025-01-24 10:00": ["click", "add_to_cart", "purchase"],
3+
"2025-01-24 10:05": ["click", "page_view"],
4+
"2025-01-24 10:10": ["page_view", "click", "add_to_cart"]
5+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import json
2+
3+
4+
def load_log_file(file_path):
5+
with open(file_path, mode="r", encoding="utf-8") as event_log_file:
6+
return json.load(event_log_file)
7+
8+
9+
def format_event_log(event_log):
10+
lines = []
11+
for timestamp, events in event_log.items():
12+
# Convert the events list to a string separated by commas.
13+
event_list_str = ", ".join(events)
14+
# Create a single line string.
15+
line = f"{timestamp} => {event_list_str}"
16+
lines.append(line)
17+
18+
# Join all lines with a newline separator.
19+
return "\n".join(lines)
20+
21+
22+
if __name__ == "__main__":
23+
log_file_path = "event_log.json"
24+
event_log = load_log_file(log_file_path)
25+
output = format_event_log(event_log)
26+
print(output)

0 commit comments

Comments
 (0)