Help needed to retrieve text from a text-file using RegEx

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Mon Feb 9 12:12:14 EST 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
> 
> Text-file:
> -------------
> AbcManager=C:\source\code\Modules\Code-AbcManager\
> AbcTest=C:\source\code\Modules\Code-AbcTest\
> DecConnector=C:\source\code\Modules\Code-DecConnector\
> GHIManager=C:\source\code\Modules\Code-GHIManager\
> JKLConnector=C:\source\code\Modules\Code-JKLConnector
> 
> -------------
> 
> So now if I run the program and user enters
> 
> DecConnector
> 
> 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
> time
> 
>  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.

>                         tempStr=Path
> 
>                         Path=tempStr.replace(mName+'=',"",1)

You don't need the temporary variable here. Also, you may want to use 
str.split instead:


# 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(';'):
             continue

         # faster and simpler than a regexp
         if line.startswith(target):
             # since the '=' is in target, we can safely assume
             # that line.split('=') will return at least a
             # 2-elements list
             path = line.split('=')[1]
             # no need to look further
             break
     else:
         # 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 mailing list