1

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?

1
  • 1
    Welcome to Stackoverflow. Please show us what you have tried and format your question appropriately (ie make code appear code). Commented Jan 8, 2021 at 13:48

4 Answers 4

1

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']
Sign up to request clarification or add additional context in comments.

1 Comment

@Sakthi: kindly accept the answer : stackoverflow.com/help/someone-answers
0

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)

Comments

0

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)

Comments

0

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'
>>> 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.