Python过滤字符串

时间:2016-08-23 23:08:42

标签: python

我正在尝试制作一个python脚本,用相应于该剧集的视频文件的文件名重命名字幕文件(.srt),所以当我在VLC中打开它时,字幕已经加载了。

到目前为止,我已成功将视频和子文件中的所有文件名都放在两个单独列表中的字符串中。现在我需要以某种方式将第1集的视频字符串与对应于同一集的字幕配对。

我尝试了许多不同的方法,但我总是,大多数都使用常规模式,但没有一个有效。 这是我的代码示例:

import glob 

videoPaths = glob.glob(r"C:\Users\tobia_000\Desktop\BBT\Video\*.mp4")
subsPaths = glob.glob(r"C:\Users\tobia_000\Desktop\BBT\Subs\*.srt") 
#With glob I sort the filenames of video and subs in separate variables

ep1 = []
ep2 = []
ep3 = []
#etc..

这就是videoPaths和subsPaths变量与我拥有的文件的外观。我还没有完成它们。

videoPath = ["The.Big.Bang.Theory.S05E24.HDTV.x264-LOL.mp4",
"The.Big.Bang.Theory.S05E19.HDTV.x264LOL.mp4",
"The.Big.Bang.Theory.S05E21.HDTV.x264-LOL.mp4"]

subsPath = ["The Big Bang Theory - 5x19 - The Weekend Vortex.720p HDTV.lol.en.srt",
"The Big Bang Theory - 5x21 - The Hawking Excitation.HDTV.LOL.en.srt",
"The Big Bang Theory - 5x24 - The Countdown Reflection.HDTV.LOL.en.srt"]

1 个答案:

答案 0 :(得分:3)

您可以使用zip来制作对,如果 videoPathssubsPaths的排序列表在彼此对应的情况下是正确的。即第一集视频和字幕都是每个列表的第一个元素。

episodes = list(zip(videoPaths, subsPaths))

编辑:由于glob以任意顺序返回结果,因此在此之前进行排序。

episodes = list(zip(sorted(videoPaths), sorted(subsPaths)))