User:Emily/Prototyping/Trimester 03/02: Difference between revisions

From XPUB & Lens-Based wiki
(Created page with "<source lang=python> with open("state.en.srt") as f: lines = f.readlines() times_texts = [] current_times , current_text = None, "" for line in lines:...")
 
No edit summary
 
(One intermediate revision by the same user not shown)
Line 15: Line 15:
             current_text = current_text + line.replace("\n"," ")
             current_text = current_text + line.replace("\n"," ")
     return times_texts
     return times_texts
</source lang=python>
</source>
apply above script to multiple files add below on top -> def (defining functions of your own) -> def function_name():
apply above script to multiple files add below on top -> def (defining functions of your own) -> def function_name():
<source>
<source lang=python>
def readsrt(srtpath):
def readsrt(srtpath):
     with open(srtpath, "rU") as f:
     with open(srtpath, "rU") as f:
Line 34: Line 34:
     tt=readsrt(p)
     tt=readsrt(p)
</source>
</source>
loop usage in python
for loop -> repeat n number of times of the code
while loop ->  a condition is to be met, or if you want a piece of code to repeat forever, for example
more about for loop
nested loops
<source lang=python>
for x in xrange(1, 11):
    for y in xrange(1, 11):
        print ...
</source>
early exit
<source lang=python>
for x in xrange(3):
    if  x == 1:
        break
</source>
for else
<source lang=python>
for x in xrange(3):
    print x
else:
    print 'Final x = %d' %(x)
</source>
----
About Regular Expression:
re.I (ignore case)
re.M(multiple lines)
x = re.search(pat, text, flags=re.I|re.M)

Latest revision as of 19:28, 20 April 2015

with open("state.en.srt") as f:
    lines = f.readlines()

    times_texts = []
    current_times , current_text = None, ""
    for line in lines:
        times = re.findall("[0-9]*:[0-9]*:[0-9]*,[0-9]*", line)
        if times != []:
            current_times = map(convert_time, times)
        elif line == '\n':
            times_texts.append((current_times, current_text))
            current_times, current_text = None, ""
        elif current_times is not None:
            current_text = current_text + line.replace("\n"," ")
    return times_texts

apply above script to multiple files add below on top -> def (defining functions of your own) -> def function_name():

def readsrt(srtpath):
    with open(srtpath, "rU") as f:
        lines = f.readlines()

sys.argv command line arguments -> passed to a python script

argv[0] is the script name

pat = sys.argv[1] #the pattern I search
time = 0
for p in sys.argv[2:]: #the srt files I search in
    print p
    tt=readsrt(p)

loop usage in python

for loop -> repeat n number of times of the code

while loop -> a condition is to be met, or if you want a piece of code to repeat forever, for example

more about for loop

nested loops

for x in xrange(1, 11):
    for y in xrange(1, 11):
        print ...

early exit

for x in xrange(3):
    if  x == 1:
        break

for else

for x in xrange(3):
    print x
else:
    print 'Final x = %d' %(x)

About Regular Expression:

re.I (ignore case)

re.M(multiple lines)

x = re.search(pat, text, flags=re.I|re.M)