How to Extract Date from a String in Python?This article will examine various techniques for obtaining dates in Python from a given string. The problem will be thoroughly explained before a variety of potential solutions are explored. Date Extraction from a StringProblem: We possess a string that displays the date in the format 'YYYY-MM-DD'. To extract the date from this string is our aim. Solution: There are various approaches that we can use in order to extract the date from the string. Some of these approaches are listed below:
Let us now discuss these methods in the following sections with the help of suitable examples. Method 1: Using re.search() + strptime() MethodsIn the following method, we will firstly check the entire string and search for the date using the search() method. After that we will use the strptime() to check if the searched string matches the required format. If so, we will print the date. Output: The org str is : I was born on 2003-10-09 in Maplewood Computed date: 2003-10-09 Explanation: The code example finds a date in a text using regular expressions and uses that date to create a datetime object. The code, for instance, finds and retrieves the date and prints it in the statement. Method 2: Using python-dateutil() ModuleOutput: The org str is : I was born on 2003-10-09 in Maplewood Computed date : 2003-10-09 Explanation: This Python script demonstrates how to extract a date from a string using the python-dateutil module. It sets up a test string with a date in a format that is easily recognized. The date is extracted from the text using the parser.parse() method from python-dateutil after the original string has been printed. Flexible parsing is enabled by the fuzzy=True argument. Lastly, the extracted date is printed. Method 3: Using String ManipulationApproach: To find the date format string in the input string, we can use string manipulation. Algorithm:
Output: The org str is : I was born on 2003-10-09 in Maplewood The Extracted Date is : 2003-10-09 Explanation: This program written in Python divides an input string into distinct words using the split() method, and iterates through each word. Within the loop, the program checks if the letter of the word is ten and has hyphens as the characters at positions 4 and 7, then that word indicate dates in the 'YYYY-MM-DD' format. When one of pattern is found, the loop is terminated, and the word is printed.
Method 4: Using Split and JoinApproach: This method divides the string into a list of words first, then separates the date from the last word. Finally, it splits the date using '-' and joins it once again using '-'. Algorithm:
Output: The org str is : I was born on 2003-10-09 Computed date: 2003-10-09 Explanation: By utilizing the. split() function to split the string into words and choose the final word with [-1]; this Python script extracts a date from the end of a string. It then uses - to break this word into its component pieces and - once more to connect them. Lastly, the extracted date is printed.
Method 5: Using Regular ExpressionApproach: Using a regular expression, the program takes the date out of a supplied text. Algorithm:
Output: The org str is : I was born on 2003-10-09 in Maplewood Computed date: 2003-10-09 Explanation: Utilizing the re module, this Python script finds a date inside a string that follows the 'YYYY-MM-DD' design. It chooses the first pattern it finds in the text, such as "2003-10-09."
|
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India