I have some simple python code that searches files for a string e.g. path=c:\path
, where the c:\path
part may vary. The current code is:
def find_path(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 the text after Path=
?
You can also remove a specified character or substring from a string by calling the String. Replace(String, String) method and specifying an empty string (String. Empty) as the replacement. The following example removes all commas from a string.
The first and most commonly used method to remove/replace any substring is the replace() method of Java String class. The first parameter is the substring to be replaced, and the second parameter is the new substring to replace the first parameter.
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 =.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With