🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
The strptime function in Python's time module parses a string representing a time according to a format and returns a struct_time object. This function is useful for converting formatted time strings into structured time data.
Table of Contents
- Introduction
strptimeFunction Syntax- Examples
- Basic Usage
- Parsing Different Time Formats
- Real-World Use Case
- Conclusion
Introduction
The strptime function in Python's time module converts a string representing a time to a struct_time object based on a specified format. This is useful for parsing time and date information from strings.
strptime Function Syntax
Here is how you use the strptime function:
import time
time.strptime(string, format)
Parameters:
string: The string representing the time.format: The format string that specifies how the time is represented in the string. This should correspond to the structure of the time string.
Returns:
- A
struct_timeobject representing the parsed time.
Common Format Codes:
%Y: Year with century (e.g., 2021)%m: Month as a zero-padded decimal number (e.g., 08)%d: Day of the month as a zero-padded decimal number (e.g., 17)%H: Hour (24-hour clock) as a zero-padded decimal number (e.g., 15)%M: Minute as a zero-padded decimal number (e.g., 43)%S: Second as a zero-padded decimal number (e.g., 06)%A: Full weekday name (e.g., Tuesday)%B: Full month name (e.g., August)
Examples
Basic Usage
Here is an example of how to use strptime.
Example
import time
# Parsing a formatted time string
time_string = "2021-08-17 15:43:06"
parsed_time = time.strptime(time_string, "%Y-%m-%d %H:%M:%S")
print("Parsed time:", parsed_time)
Output:
Parsed time: time.struct_time(tm_year=2021, tm_mon=8, tm_mday=17, tm_hour=15, tm_min=43, tm_sec=6, tm_wday=1, tm_yday=229, tm_isdst=-1)
Parsing Different Time Formats
This example shows how to parse time strings in different formats.
Example
import time
# Parsing a time string with a different format
time_string = "17/08/2021 15:43:06"
parsed_time = time.strptime(time_string, "%d/%m/%Y %H:%M:%S")
print("Parsed time:", parsed_time)
# Parsing another time string with month name and day of the week
time_string = "Tuesday August 17 15:43:06 2021"
parsed_time = time.strptime(time_string, "%A %B %d %H:%M:%S %Y")
print("Parsed time:", parsed_time)
Output:
Parsed time: time.struct_time(tm_year=2021, tm_mon=8, tm_mday=17, tm_hour=15, tm_min=43, tm_sec=6, tm_wday=1, tm_yday=229, tm_isdst=-1)
Parsed time: time.struct_time(tm_year=2021, tm_mon=8, tm_mday=17, tm_hour=15, tm_min=43, tm_sec=6, tm_wday=1, tm_yday=229, tm_isdst=-1)
Real-World Use Case
Parsing Log Timestamps
In real-world applications, the strptime function can be used to parse timestamps from log files and convert them into structured time data for further processing.
Example
import time
def parse_log_timestamp(log_line):
timestamp_str = log_line.split(' - ')[0]
return time.strptime(timestamp_str, "%Y-%m-%d %H:%M:%S")
# Example usage
log_line = "2021-08-17 15:43:06 - Event: Start process"
parsed_timestamp = parse_log_timestamp(log_line)
print("Parsed timestamp:", parsed_timestamp)
Output:
Parsed timestamp: time.struct_time(tm_year=2021, tm_mon=8, tm_mday=17, tm_hour=15, tm_min=43, tm_sec=6, tm_wday=1, tm_yday=229, tm_isdst=-1)
Exception ignored in tp_clear of: <class 'dict'>
TypeError: Missed attribute 'n_fields' of type time.struct_time
Conclusion
The strptime function in Python's time module parses a string representing a time according to a format and returns a struct_time object. This function is useful for converting formatted time strings into structured time data.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment