-
-
Save HyShai/a2bd2f59cf6b023a1b93 to your computer and use it in GitHub Desktop.
scores.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
scores = [[u'Orlando 81 Washington 90 (3:55 IN 4TH)'], [u'Atlanta 59 Cleveland 87 (3:51 IN 3RD)'], [u'Utah 62 Toronto 69 (3:59 IN 3RD)'], [u'Indiana 46 Chicago 42 (0:03 IN 2ND)'], [u'Detroit 50 Memphis 51 (0:18 IN 2ND)'], [u'Minnesota 22 Dallas 28 (0:00 IN 1ST)'], [u'Brooklyn at Portland (10:00 PM ET)'], [u'San Antonio at Sacramento (10:00 PM ET)'], [u'Charlotte at Golden State (10:30 PM ET)'], [u'Phoenix at LA Clippers (10:30 PM ET)']] | |
for score in scores: | |
print score | |
print re.sub('([a-zA-Z^ ]+?)(\\d+|at)\\s+?([a-zA-Z^ ]+?)(\\d+)?\\s+?(\\(.+\\))\\s+?', 'whatever replacment', score[0]) |
@cclauss thanks! That is nicer. In practice though I'm getting the data from parsing a query string using the urllib.parse_qs
method which returns an array for me. But for a contrived example your approach is easier to read. Thanks for the tip
A non-regex approach...
for score in scores.splitlines():
print(score)
teams, _, times = score.strip().rstrip(')').partition('(')
if ' at ' in teams:
away_team, home_team = teams.strip().split(' at ')
away_score, home_score = 'at', ''
else:
away_team, home_team = teams.strip().split(' ')
away_team, away_score = away_team.rsplit()
home_team, home_score = home_team.rsplit()
print(away_team, away_score, home_team, home_score, times)
print('=' * 5)
print('=' * 15)
@cclaus - hmmm interesting - that's definitely more readable than my regex
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is an alternate way to represent your scores data: