User:Emily/Prototyping/Trimester 03/02

From XPUB & Lens-Based wiki
< User:Emily
Revision as of 17:07, 20 April 2015 by Emily (talk | contribs)
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 <source lang=python> for x in xrange(3):

   print x

else:

   print 'Final x = %d' %(x)

</python>