[Tutor] Which string operation is best to extract text from database string?

Emile van Sebille emile at fenx.com
Wed Oct 27 19:49:41 CEST 2010


On 10/27/2010 9:45 AM Sean Carolan said...
> Forgive me for the newbie question; I'm sure there must be an easy way
> to do this in python:
>
> I have a database string that I'm grabbing from a file with readlines():
>
> db.url=jdbc:oracle:thin:@(DESCRIPTION = (ADDRESS = (PROTOCOL =
> TCP)(HOST = server.company.com)(PORT =1521)) (ADDRESS = (PROTOCOL =
> TCP)(HOST = server.company.com)(PORT = 1521)) (ADDRESS = (PROTOCOL
> =TCP)(HOST = server.company.com)(PORT = 1521)) (LOAD_BALANCE = ON)
> (FAILOVER = ON) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME =
> PROD2) (FAILOVER_MODE = (TYPE = SELECT) (METHOD = BASIC) (RETRIES =
> 180) (DELAY = 5))))
>
> I want to extract the service name, PROD2 from the string.  Normally
> I'd do this with sed or awk, but I'm trying to get away from using
> OS-dependent tools with Popen, etc.  What would be the most efficient
> way to grab "PROD2" from this string of text?


Assuming you've got that string in variable text, and that "SERVICE_NAME 
=" is there to be found, I'd do something like

servicename = text.split("SERVICE_NAME =")[1].split(")")[0]

Emile



More information about the Tutor mailing list