User:Emily/Prototyping/Trimester 03/02

From XPUB & Lens-Based wiki
< User:Emily
Revision as of 19:28, 20 April 2015 by Emily (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
with open("") 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

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:

for else

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

About Regular Expression:

re.I (ignore case)

re.M(multiple lines)

x =, text, flags=re.I|re.M)