[英]How to extract specific content from the string value in python
I am new to python.我是 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:我需要的 Output:
2020-10-02 13-34-55
Could anyone help me to solve this in python?谁能帮我在 python 中解决这个问题?
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.请注意,索引
-1
是最后一个 position,或者等效地从右起第一个。
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)
You can use os.path.basename
to get filename.您可以使用
os.path.basename
来获取文件名。 then you need to use os.path.splitext
to separate extension from filename那么您需要使用
os.path.splitext
将扩展名与文件名分开
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)
Since your string appears to be a path value由于您的字符串似乎是路径值
You can also utilize os.path您还可以使用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'
>>>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.