From 17e7a8b6e0b440ed4dcc8e04a9be4dcac378eec3 Mon Sep 17 00:00:00 2001 From: martin-martin Date: Mon, 20 Jan 2025 09:31:06 +0100 Subject: [PATCH 1/2] Add code examples for string split tutorial --- split-strings/README.md | 3 +++ split-strings/extract_errors_from_log.py | 9 +++++++++ split-strings/extract_log_info.py | 8 ++++++++ split-strings/names_with_titles.py | 8 ++++++++ split-strings/split_logs_regex.py | 7 +++++++ 5 files changed, 35 insertions(+) create mode 100644 split-strings/README.md create mode 100644 split-strings/extract_errors_from_log.py create mode 100644 split-strings/extract_log_info.py create mode 100644 split-strings/names_with_titles.py create mode 100644 split-strings/split_logs_regex.py diff --git a/split-strings/README.md b/split-strings/README.md new file mode 100644 index 0000000000..2be180a519 --- /dev/null +++ b/split-strings/README.md @@ -0,0 +1,3 @@ +# How to Split a String in Python + +This folder provides the code examples for the Real Python tutorial [How to Split a String in Python](https://realpython.com/python-string-split-concatenate-join/). diff --git a/split-strings/extract_errors_from_log.py b/split-strings/extract_errors_from_log.py new file mode 100644 index 0000000000..00725e0483 --- /dev/null +++ b/split-strings/extract_errors_from_log.py @@ -0,0 +1,9 @@ +log_data = """2025-01-15 08:45:23 INFO User logged in +2025-01-15 09:15:42 ERROR Failed to connect to server +2025-01-15 10:01:05 WARNING Disk space running low""" + +log_lines = log_data.splitlines() + +for line in log_lines: + if "ERROR" in line: + print(line) diff --git a/split-strings/extract_log_info.py b/split-strings/extract_log_info.py new file mode 100644 index 0000000000..fa3539880c --- /dev/null +++ b/split-strings/extract_log_info.py @@ -0,0 +1,8 @@ +log_line = "2025-01-15 08:45:23 INFO User logged in from IP 10.0.1.1" + +date, time, log_level, message = log_line.split(maxsplit=3) + +print(f"Date: {date}") +print(f"Time: {time}") +print(f"Log Level: {log_level}") +print(f"Message: {message}") diff --git a/split-strings/names_with_titles.py b/split-strings/names_with_titles.py new file mode 100644 index 0000000000..deb062fcbe --- /dev/null +++ b/split-strings/names_with_titles.py @@ -0,0 +1,8 @@ +person = "Dr. Jane Doe, PhD Professor of Biology" + +title, first_name, last_name, rest_info = person.split(maxsplit=3) + +print(f"Title: {title}") +print(f"First Name: {first_name}") +print(f"Last Name: {last_name}") +print(f"Additional Info: {rest_info}") diff --git a/split-strings/split_logs_regex.py b/split-strings/split_logs_regex.py new file mode 100644 index 0000000000..4706b0e64d --- /dev/null +++ b/split-strings/split_logs_regex.py @@ -0,0 +1,7 @@ +import re + +log_line = "2025-01-15 08:45:23 INFO User logged in from IP 10.0.1.1" +pattern = r"(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})\s(\w+)\s" +components = re.split(pattern, log_line) + +print(components) From 19b1ee1a3192013f3ae8ce2ac2743dacbbadc17a Mon Sep 17 00:00:00 2001 From: martin-martin Date: Fri, 24 Jan 2025 12:51:58 +0100 Subject: [PATCH 2/2] Update code examples after TR --- split-strings/names_with_titles.py | 8 -------- split-strings/split_logs_regex.py | 7 ------- split-strings/split_shopping_list_regex.py | 6 ++++++ 3 files changed, 6 insertions(+), 15 deletions(-) delete mode 100644 split-strings/names_with_titles.py delete mode 100644 split-strings/split_logs_regex.py create mode 100644 split-strings/split_shopping_list_regex.py diff --git a/split-strings/names_with_titles.py b/split-strings/names_with_titles.py deleted file mode 100644 index deb062fcbe..0000000000 --- a/split-strings/names_with_titles.py +++ /dev/null @@ -1,8 +0,0 @@ -person = "Dr. Jane Doe, PhD Professor of Biology" - -title, first_name, last_name, rest_info = person.split(maxsplit=3) - -print(f"Title: {title}") -print(f"First Name: {first_name}") -print(f"Last Name: {last_name}") -print(f"Additional Info: {rest_info}") diff --git a/split-strings/split_logs_regex.py b/split-strings/split_logs_regex.py deleted file mode 100644 index 4706b0e64d..0000000000 --- a/split-strings/split_logs_regex.py +++ /dev/null @@ -1,7 +0,0 @@ -import re - -log_line = "2025-01-15 08:45:23 INFO User logged in from IP 10.0.1.1" -pattern = r"(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})\s(\w+)\s" -components = re.split(pattern, log_line) - -print(components) diff --git a/split-strings/split_shopping_list_regex.py b/split-strings/split_shopping_list_regex.py new file mode 100644 index 0000000000..9c70ab1f86 --- /dev/null +++ b/split-strings/split_shopping_list_regex.py @@ -0,0 +1,6 @@ +import re + +shopping_mess = "Apple :::::3:Orange | 2|||Lemon --1-Date :: 10" +shopping_list = re.split(r"\s*[:|-]+\s*", shopping_mess) + +print(shopping_list)