I am trying to search this text document with a regular expression but I also need a users input in the regular expression, I am unsure how to do this and am quite new to python programming and any assistance will be greatly appreciated !
import re
str1=raw_input("Room Number(L1 - L5): ")
text=open('roombookings.txt', 'r')
while 1:
line = text.readline()
if line:
re.search(?????)
else:
break
text.close()
To iterate over the lines in a file, do
You don’t need any of that
while 1stuff.To “splice” the contents of a variable into a string, do
See the docs.
To open and close files, do:
instead of manually writing
f.close()— this handles exceptions as well. Note also that if you don’t explicitly close the file it will be closed when the object is deleted, which happens when there are no more references to it.