Newbie Question: Regular Expressions
Bjorn Pettersen
BPettersen at NAREX.com
Thu Jul 12 12:06:58 EDT 2001
> From: fett at tradersdata.com [mailto:fett at tradersdata.com]
>
> I have a really dumb program that i would like to make
> smarter. I need
> to take a file on my hard drive and filter out everything
> except for the
> standings which are written in it. I have tried to use regular
> expressions with no success, but i still think that they are probably
> the best way. I created the following simple fix, but it is
> unreliable
> if the data changed posistions.
>
>
> input = open('rawdata', 'r')
> S = input.read()
> print S[4021:6095]
>
> Output :
> League Standings
> American League
> EAST W L PCT GB HOME ROAD EAST CENT WEST NL L10 STRK
> Red Sox 43 29 .597 - 23-15 20-14 23-13 8-7 6-6 6-3 6-4 L2
> Yankees 41 31 .569 2.0 21-15 20-16 19-11 12-9 5-7 5-4 6-3 W2
> Blue Jays 35 38 .479 8.5 18-20 17-18 14-13 6-7 11-13 4-5 5-5 W3
> Orioles 34 39 .466 9.5 20-20 14-19 15-17 9-12 6-5 4-5 5-5 L1
> ........( it continues with all the standings)
How about something like:
def findTeam(team, lines):
for line in lines:
if line.startswith(team):
print line
break
>
> Also could you tell me if its possible to download the data from the
> web-page in python so that it doesnt even have to deal with
> opening the
> file.
import urllib
fp = urllib.urlopen('http://www.yoursite.com')
findTeam( fp.readlines() )
hth,
-- bjorn
More information about the Python-list
mailing list