User:Emily/Prototyping/Trimester 03/02: Difference between revisions
No edit summary |
No edit summary |
||
Line 60: | Line 60: | ||
else: | else: | ||
print 'Final x = %d' %(x) | 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)