diff --git a/python-join-strings/README.md b/python-join-strings/README.md new file mode 100644 index 0000000000..8c8c0a8593 --- /dev/null +++ b/python-join-strings/README.md @@ -0,0 +1,11 @@ +# How to Join a String in Python + +This folder contains code associated with the Real Python tutorial on [How to Join a String in Python](https://realpython.com/python-join-string/). + +## About the Author + +Martin Breuss - Email: martin@realpython.com + +## License + +Distributed under the MIT license. See `LICENSE` for more information. \ No newline at end of file diff --git a/python-join-strings/event_log.json b/python-join-strings/event_log.json new file mode 100644 index 0000000000..2b46d4d9fe --- /dev/null +++ b/python-join-strings/event_log.json @@ -0,0 +1,5 @@ +{ + "2025-01-24 10:00": ["click", "add_to_cart", "purchase"], + "2025-01-24 10:05": ["click", "page_view"], + "2025-01-24 10:10": ["page_view", "click", "add_to_cart"] +} \ No newline at end of file diff --git a/python-join-strings/format_event_log.py b/python-join-strings/format_event_log.py new file mode 100644 index 0000000000..9436ca17a7 --- /dev/null +++ b/python-join-strings/format_event_log.py @@ -0,0 +1,26 @@ +import json + + +def load_log_file(file_path): + with open(file_path, mode="r", encoding="utf-8") as event_log_file: + return json.load(event_log_file) + + +def format_event_log(event_log): + lines = [] + for timestamp, events in event_log.items(): + # Convert the events list to a string separated by commas. + event_list_str = ", ".join(events) + # Create a single line string. + line = f"{timestamp} => {event_list_str}" + lines.append(line) + + # Join all lines with a newline separator. + return "\n".join(lines) + + +if __name__ == "__main__": + log_file_path = "event_log.json" + event_log = load_log_file(log_file_path) + output = format_event_log(event_log) + print(output)