Sakthi
Sakthi

Reputation: 11

How to extract specific content from the string value in python

I am new to python. I am trying to extract the date and time values from a string.

string_value :

List = ['D:/Python/sfusd_to_hipSfusd/reports_api/meet/activity/dt=2020-10- 
02/api_batch_id=00db1d37-96bb-4beb-a8db-0e62443f5d81/2020-10-02 13-34-55.json']

Output that I need :

2020-10-02 13-34-55

Could anyone help me to solve this in python?

Upvotes: 1

Views: 79

Answers (4)

Vaebhav
Vaebhav

Reputation: 5032

Since your string appears to be a path value

You can also utilize os.path


>>> s =  'D:/Python/sfusd_to_hipSfusd/reports_api/meet/activity/dt=2020-10-02/api_batch_id=00db1d37-96bb-4beb-a8db-0e62443f5d81/2020-10-02 13-34-55.json'

>>> os.path.splitext(os.path.split(s)[1])[0]
'2020-10-02 13-34-55'
>>> 

Upvotes: 0

Joby Wilson Mathews
Joby Wilson Mathews

Reputation: 11116

You can use os.path.basename to get filename . then you need to use os.path.splitext to separate extension from filename

Code:

import os

string_value = ["D:/Python/sfusd_to_hipSfusd/reports_api/meet/activity/dt=2020-10- 02/api_batch_id=00db1d37-96bb-4beb-a8db-0e62443f5d81/2020-10-02 13-34-55.json"]
for str in string_value:
    file = os.path.basename(string_value[0])
    f_name, f_ext = os.path.splitext(file)
    print(f_name)

Upvotes: 0

Subbu VidyaSekar
Subbu VidyaSekar

Reputation: 2615

Use Regex to get the right pattern

import re
re.findall("([0-9]{4}\-[0-9]{2}\-[0-9]{2} [0-9]{2}-[0-9]{2}-[0-9]{2})", s)

Output:

['2020-10-02 13-34-55']

Upvotes: 1

Moritz Groß
Moritz Groß

Reputation: 1470

Of course I don't know the format of the strings you want to extract the date from. But this is a rough idea on how to do it. Note that the index -1 is the last position, or equivalently the first from right.

s =  'D:/Python/sfusd_to_hipSfusd/reports_api/meet/activity/dt=2020-10-02/api_batch_id=00db1d37-96bb-4beb-a8db-0e62443f5d81/2020-10-02 13-34-55.json'
parts_split_by_slash = s.split("/")
after_last_slash = parts_split_by_slash[-1]
part_before_dot = after_last_slash.split(".")[0]
print(part_before_dot)

Upvotes: 0

Related Questions