python-给定格式和字符串,从该字符串获取信息

python-给定格式和字符串,从该字符串获取信息,python,regex,format,Python,Regex,Format,我想做一些类似于datetime模块中的strTime方法的事情。给定格式和字符串,更改该字符串的格式并返回新字符串。例如,我有一个电视剧的一集的文件名,我调用函数来更改文件名的格式,指定标题在哪里,情节在哪里,质量等等 def change_format(file_name, format, season=1): title = # Get the title applying the format to the string episode = # Get the episo

我想做一些类似于datetime模块中的strTime方法的事情。给定格式和字符串,更改该字符串的格式并返回新字符串。例如,我有一个电视剧的一集的文件名,我调用函数来更改文件名的格式,指定标题在哪里,情节在哪里,质量等等

def change_format(file_name, format, season=1):
    title = # Get the title applying the format to the string
    episode = # Get the episode applying the format to the string
    season = # Get the season applying the format to the string or from the variable in the function
    quality # Get the quality applying the format to the string
    return f'{title} + S{season}E{episode} [{quality}]'
有些信息可能会给出,也可能不会给出(例如,如果没有给出质量,则不要在退货中使用),并且可能会有额外的信息

change_format('Some Title Here - 1 [1080p] [CDI3989AFM]', '%t - %e [%q][%i]')
这里,%t是标题,%e是插曲,%q是质量,%i是一些额外的信息。 标题有3个单词,所以你应该指定单词的数量

预期产出:

"Some Title Here S01E01 [1080p] [CDI3989AFM]"

您可以使用正则表达式进行此操作,如下所示:

重新导入
r=re.match('(...*(\[.\]).*(\[..\]),“此处的某些标题-1[1080p][CDI3989AFM]”)
r、 第(1)组#此处有一些标题-1
r、 第(2)组#[1080p]
r、 第(2)组#[CDI3989AFM]

但是会有不同格式的文件,所以我必须为每种格式制作不同的正则表达式?@用户是的,你不能只说
%t
总是标题