Google python babyname.py (python 2.7)

#!/usr/bin/python

import sys
import re

"""Baby Names exercise

Define the extract_names() function below and change main()
to call it.

For writing regex, it's nice to include a copy of the target
text for inspiration.

Here's what the html looks like in the baby.html files:
...
<h3 align="center">Popularity in 1990</h3>
....
<tr align="right"><td>1</td><td>Michael</td><td>Jessica</td>
<tr align="right"><td>2</td><td>Christopher</td><td>Ashley</td>
<tr align="right"><td>3</td><td>Matthew</td><td>Brittany</td>
...

Suggested milestones for incremental development:
-Extract the year and print it
-Extract the names and rank numbers and just print them
-Get the names data into a dict and print it
-Build the [year, 'name rank', ... ] list and print it
-Fix main() to use the extract_names list
"""

def extract_names(filename):
"""
Given a file name for baby.html, returns a list starting with the year string
followed by the name-rank strings in alphabetical order.
['2006', 'Aaliyah 91', Aaron 57', 'Abagail 895', ' ...]
"""
[color=red] if re.search(r'[0-9]{4}',filename):
year=re.search(r'[0-9]{4}',filename).group()
else:
sys.exit(1)
f=open(filename)
cont=f.read()
f.close()
dic=[]
male_dict=[]
all_dict=re.findall('<tr align="right"><td>([0-9]+)</td><td>([A-Z][a-z]+)</td><td>([A-Z][a-z]+)',cont)
for name in all_dict:
male_dict.append(name[1]+' '+name[0])
dic.append(name[2]+' '+name[0])
dic.sort()
male_dict.sort()
male_dict.insert(0,'-'*30+'Male'+'-'*30)
dic.insert(0,year)
dic.insert(1,'-'*30+'Female'+'-'*30)
dic.extend(male_dict)
return dic[/color]


def main():
# This command-line parsing code is provided.
# Make a list of command line arguments, omitting the [0] element
# which is the script itself.
args = sys.argv[1:]

if not args:
print 'usage: [--summaryfile] file [file ...]'
sys.exit(1)

# Notice the summary flag and remove it from args if it is present.
summary = False
if args[0] == '--summaryfile':
summary = True
del args[0]

[color=red] for arg in args:
names=extract_names(arg)
print '\n'.join(names)
if summary:
sf=open(names[0]+'.txt','w')
sf.write('\n'.join(names)+'\n') [/color]

if __name__ == '__main__':
main()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值