Removing a certain part of a string in python -



Removing a certain part of a string in python -

"submitted 1 year ago bagelpirate /r/books"

basically i'm learning web scraping , pulled info html of reddit page. need "bagelpirate" out of string. there way in python?

given starting string:

s = "submitted 1 year ago bagelpirate /r/books"

you (finding position of preceding , next substrings):

name = s[s.index(' ')+4:s.index(' /r/books')]

or utilize regular expression:

import re name = re.search(' (.+) /r/books', s).group(1)

that means, 'find "by (something) /r/books" in string , give me part indicated parentheses'.

it kind of depends format strings going in.

python

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -