Eye of Hell Eye of Hell - 1 year ago 48
Python Question

How to remove the left part of a string?

I have some simple python code that searches files for a string e.g.

, whereby the
may vary. The current code is:

def findPath( i_file) :
lines = open( i_file ).readlines()
for line in lines :
if line.startswith( "Path=" ) :
return # what to do here in order to get line content after "Path=" ?

What is a simple way to get a string text after
Is there a simple method, without closures, reflection or other esoteric things ?

Answer Source

If the string is fixed you can simply use:

if line.startswith("Path="):
    return line[5:]

which gives you everything from position 5 on in the string (a string is also a sequence so these sequence operators work here, too).

Or you can split the line at the first =:

if "=" in line:
    param, value = line.split("=",1)

Then param is "Path" and value is the rest after the first =.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download