Python - 使用正则表达式循环内部循环的两个文件

时间:2016-11-21 17:06:35

标签: python regex file loops

我试图创建一个循环以检查两个文件,并在特定字段匹配时与正则表达式进行比较。

avi file
TVShowName.S01E01.W.DVDRip.XviD.avi
TVShowName.S01E02.W.DVDRip.XviD.avi
TVShowName.S01E03.W.DVDRip.XviD.avi

srt
tvShowName.S01E01.episodename.DVDRip.XviD.srt
tvShowName.S01E02.episodename.DVDRip.XviD.srt
tvShowName.S01E03.episodename.DVDRip.XviD.srt

如果没有循环,我可以匹配文件并使魔法发生。虽然当我使用循环时它只到达第一行。

TVShowName.S01E01.W.DVDRip.XviD.avi
TVShowName.S01E01.W.DVDRip.XviD.srt

代码:

f1 = open('avi', 'r')
f2 = open('srt', 'r')
f3 = open ('merge', 'a')
for avi in f1:
    m = re.search(".*([Ss][0-20].[eE][0-24].)+.*", avi )

    for sub in f2:
        n = re.search(".*([Ss][0-20].[eE][0-24].)+.*", sub )
        if m.group(1) == n.group(1):

            str_avi = str(m.group(0))
            #print str_avi

            ext_srt = str_srt.split('.')
            ext_avi = str_avi.split('.')

            #print  ext_avi
            #conv_str = str(m.group(0))
            merge = str_avi.replace(ext_avi[-1],ext_srt[-1])  
            print merge
            f3.write(merge)
f3.close()

2 个答案:

答案 0 :(得分:0)

我不完全确定这是否是您想要的输出。我无法添加评论,因为我没有足够的声誉点。

import glob
import re
avifiles = []
srtfiles = []
for afile in glob.glob('*.avi'):
    avifiles.append(afile)
for sfile in glob.glob('*.srt'):
    srtfiles.append(sfile)
#f1 = open('avi', 'r')
#f2 = open('srt', 'r')
f3 = open ('merge', 'a')
for avi in avifiles:
    m = re.search(".*([Ss][0-20].[eE][0-24].)+.*", avi )

for sub in srtfiles:
    n = re.search(".*([Ss][0-20].[eE][0-24].)+.*", sub )
    if m.group(1) == n.group(1):

        str_avi = str(m.group(0))
        str_srt = str(n.group(0))

        ext_srt = str_srt.split('.')
        ext_avi = str_avi.split('.')

        #print  ext_avi
        #conv_str = str(m.group(0))
        merge = str_avi.replace(ext_avi[-1],ext_srt[-1])  
        print merge
        f3.write(merge+"\n")
f3.close()

答案 1 :(得分:0)

我制作了以下代码,看起来很有效。我的下一步是添加更多视频扩展。但它应该很容易。 谢谢你们帮忙!

import re, os, sys, itertools

str_avi = ''
split_avi = ''
global zzz
lista_avi = []
lista_srt = []
lista_final = []
os.chdir('.')
f1 = os.listdir(".")


for full in f1:
    avi = re.search(".*([Ss][0-9].[eE][0-9].)+.*(...$)", full )
    if avi:

        if avi.group(2) == 'avi':
            lista_avi.append(avi.group(0))
        elif avi.group(2) == 'srt':
            lista_srt.append(avi.group(0))
        else:
            pass
    else:
        print "Nenhum Arquivo localizado!"      

for f,b in itertools.izip(lista_avi,lista_srt): 
    data_avi = f.split('.')     
    data_srt = b.split('.')
    data_regx_avi = re.search(".*([Ss][0-9].[eE][0-9].)+.*(...$)", f )
    data_regx_srt = re.search(".*([Ss][0-9].[eE][0-9].)+.*(...$)", b )
    for x in lista_srt:
        data_regx_srt = re.search(".*([Ss][0-9].[eE][0-9].)+.*(...$)", x )
        if data_regx_avi.group(1) == data_regx_srt.group(1):
            print 'Arquivo video:', data_regx_avi.group(0)
            print 'Arquivo sub:  ', f.replace(data_avi[-1],data_srt[-1])
            #lista_final.append(f.replace(data_avi[-1],data_srt[-1])) 
            xx = f.replace(data_avi[-1],data_srt[-1])
            os.rename(x, xx)