0

This is a very basic question, but I'm very new to python. I want to read data in a file. The data is between two delimiters. I'm not exactly sure how I can extract the data and skip the delimiters. Any help would be great.

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by vegaseat
0

You can use this little function ...

def extract(text, sub1, sub2):
    """
    extract a substring from text between first
    occurances of substrings sub1 and sub2
    """
    return text.split(sub1, 1)[-1].split(sub2, 1)[0]

str1 = "<a href='http://www.google.com'>more stuff here</a>"
str2 = extract(str1, "='", "'>")

print( str1 )
print( str2 )  # http://www.google.com

Edited by vegaseat: p3

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.