Help needed to retrieve text from a text-file using RegEx
bdesth.quelquechose at free.quelquepart.fr
Mon Feb 9 18:12:14 CET 2009
Oltmans a écrit :
> Here is the scenario:
> It's a command line program. I ask user for a input string. Based on
> that input string I retrieve text from a text file. My text file looks
> like following
> So now if I run the program and user enters
> Then I'm supposed to show them this text "C:\source\code\Modules\Code-
> DecConnector" from the text-file. Right now I'm retrieving using the
> following code which seems quite ineffecient and inelegant at the same
> with open('MyTextFile.txt')
This will lookup for MyFile.txt in the system's current working
directory - which is not necessarily in the script's directory.
> as file:
this shadows the builtin's 'file' symbol.
> for line in file:
> if mName in line: #mName is the string that
> contains user input
> Path =str(line).strip('\n')
'line' is already a string.
You don't need the temporary variable here. Also, you may want to use
# NB : renaming for conformity to
# Python's official naming conventions
# 'name' => what the user looks for
# 'path_to_file' => fully qualified path to the 'database' file
target = "%s=" % name # what we are really looking for
with open(path_to_file) as the_file:
for line in the_file:
# special bonus : handles empty lines and 'comment' lines
# feel free to comment out the thre following lines if
# you're sure you don't need them !-)
line = line.strip()
if not line or line.startswith('#') or line.startswith(';'):
# faster and simpler than a regexp
# since the '=' is in target, we can safely assume
# that line.split('=') will return at least a
# 2-elements list
path = line.split('=')
# no need to look further
# target not found...
path = None
> I was wondering if using RegEx will make this look better.
I don't think so. Really.
More information about the Python-list