2

I have a Python list from some web scraping that looks like the list below.

['\n           Elementary School: HUTCHINSON \n           High School: RIVERSIDE \n           Middle School: ROLLING RIDGE \n  ... ] 

My goal is to retrieve the high school name of each listing I pull. For this particular listing, the high school is RIVERSIDE. What is the best way to extract this information?

My goal is to have a variable hs = "RIVERSIDE" for this particular listing.

Side note: this data is from housing listings on realtor.com

1 Answer 1

1

You can try this:

ls=['\n           Elementary School: HUTCHINSON \n           High School: RIVERSIDE \n           Middle School: ROLLING RIDGE \n ']


hs=[i.strip().split(':')[1].replace('\n','') for i in ls[0].split('\n') if 'High School' in i][0]
print('hs =',hs)

Output:

hs =  RIVERSIDE

Or you can use re:

import re 
highschoolregex = re.compile(r'(High School)[:] (\w.+)') 
hs = highschoolregex.search(ls[0]).group(2)
print('hs =',hs)

Output:

hs =  RIVERSIDE
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.