F
F
FedOTs2014-02-07 00:34:36
Python
FedOTs, 2014-02-07 00:34:36

How can a Python script be optimized?

The script connects rus and eng subtitles.

f1 = open('file_eng.srt','r')
f2 = open('file_rus.srt','r')
fin = open('file_all.srt','w')
s = list(f1.readlines())
s1 = list(f2.readlines())
s3=[]
k=1
for i in s:
    for j in s1:
        if len(i)>4 and len(j)>4 and i==j and '00' in i and '00' in j:
            s3.append(str(k)+'\n')
            s3.append(i)
            k+=1
#print (s3)
l=2
k=0
j=1
while j<len(s):
    if (len(s[k])>4 and len(s[j])>4 or not s[k].replace('\n','').isdigit() and not s[j].replace('\n','').isdigit()) and '00' not in s[k] and s[k] not in s3[l-4:l]:
        #print (i.replace('\n',''))
        if (len(s[k])>4 and len(s[j])>4 or not s[k].replace('\n','').isdigit() and not s[j].replace('\n','').isdigit()) and '00' not in s[j]:
            s3.insert(l, s[j])
       # else:
       #     s3.insert(l, '.')
        s3.insert(l, s[k])

        l += 4
    k += 1
    j += 1
if (len(s[-1])>4 or not s[-1].replace('\n','').isdigit()) and '00' not in s[-1]:
    s3.append(s[-1])
    if (len(s[-2])>4 or not s[-2].replace('\n','').isdigit())and '00' not in s[-2]:
        s3.append(s[-2])

l=4
k=0
j=1
while j<len(s1):
    if (len(s1[k])>4 and len(s1[j])>4 or not s1[k].replace('\n','').isdigit() and not s1[j].replace('\n','').isdigit()) and '00' not in s1[k] and s1[k] not in s3[l-6:l]:
        #print (i.replace('\n',''))
        s3.insert(l, '\n')
        if (len(s1[k])>4 and len(s1[j])>4 or not s1[k].replace('\n','').isdigit() and not s1[j].replace('\n','').isdigit()) and '00' not in s1[j]:
            s3.insert(l, s1[j])
        #else:
        #    s3.insert(l, '.')
        s3.insert(l, s1[k])
        l += 7
    k += 1
    j += 1
if (len(s1[-1])>4 or not s1[-1].replace('\n','').isdigit()) and '00' not in s1[-1]:
    s3.append(s1[-1])
    if (len(s1[-2])>4 or not s1[-2].replace('\n','').isdigit())and '00' not in s1[-2]:
        s3.append(s1[-2])

for i in s3:
    fin.write(i)

f1.close()
f2.close()
fin.close()

Made hastily, first we tear out the timings, then we insert the words.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
J
JRazor, 2014-02-07
@FedOTs

You do not have a script, but a demonstration of unpythonic style. How many times to repeat - you do not use the variables m, n and so on. Every textbook already says this. How can you read your code? No way!
By the subject: isn't it easier to just parse every n-th line? Removing extra RegExp'om? Dividing into a list by "\n" and selecting only subtitles?

K
Konstantin Dovnar, 2014-02-07
@SolidlSnake

I agree, they obviously came from the C \ C ++ language. :)
The for loop can (and should) be used to traverse lines of files:

# Нихт-нихт, не делайте так
f = open("bar.txt")
s = list(f1.readlines())
for i in  s:
   print i

# Я, я, зис ис гуд
f = open("foo.txt")
for l in f:
    print s

The conditions of your if -statements also look creepy, excessively large, make a replacement right there - although you can do this carefully in advance.
In general, for the beauty of the code, read the eighth PEP ( eng / rus ), a wonderful thing. :)

M
MagNet, 2014-02-08
@MagNet

There is already ready, why your bike? For example: https://github.com/wistful/srtmerge

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question