PYTHON核心编程第三版 第一章习题答案(1.1~1.29)

这份习题答案是在看书过程中完成的,并不是标准答案,不过大部分我都验证过,应该没问题。

我使用的环境是 python 2.7.14

完成过程中,主要参考了这位博主的文章

https://blog.csdn.net/python_dream/article/details/78669906

接下来就是习题答案了,是用 markdown 格式完成的,在 CSDN 上不能很好地显示

### 1-1
[bh][aiu]t

### 1-2
[A-Za-z-]+ [A-Za-z-]+
> 认为名字中不会出现数字或其他符号,但可以是连字符

### 1-3
[A-Za-z-]+, [A-Za-z]

### 1-4
[A-Za-z_][\w]+
> [\w] 相当于 [A-Za-z0-9_]

> Python标识符必须以字母或下划线开头,后面跟字母、下划线或者数字,且标识符不能为关键字(对于怎么避免匹配到关键字,现在还没有想法)

### 1-5
\d+ (\s\w+)+

### 1-6
www[\.\w+]+.com
www[\.\w+]+\.(com|edu|net)

### 1-7
[\+-]?\d+

### 1-8
[\+-]?\d+L

### 1-9
[\+-]?\d+(\.\d+)?
> 不支持 1. 或者 .1 的写法

### 1-10
(([\+-]?\d+(\.\d+)?)?)(([\+-]?\d+(\.\d+)?)i)

### 1-11
\w+@[\w+\.]+(com|edu\.cn|net)
> 格式为:用户名@域名

### 1-12
[\w-]+(\.[\w-]+)+

> 1、URL请求采用小写字母,数字,部分特殊符号(非制表符)组成。

> 2、URL请求中不采用大小写混合的驼峰命名方式,尽量采用全小写单词,如果需要连接多个单词,则采用连接符“_”连接单词

### 1-13
\s'(.+)'

### 1-14
1[0-2]

### 1-15
\d{4}-\d{4,5}-\d{4,6}(-\d{4})?

### 1-16
```
#!/usr/bin/env python

from random import randrange, choice
from string import ascii_lowercase as lc
from sys import maxint
from time import ctime

tlds = ('com', 'edu', 'net', 'org', 'gov')

with open('redata.txt', 'w') as fp:
    for i in xrange(randrange(5,11)):
        dtint = randrange(maxint)
        dtstr = ctime(dtint)
        llen  = randrange(4,8)
        login = ''.join(choice(lc) for j in range(llen))
        dlen  = randrange(llen, 13)
        dom   = ''.join(choice(lc) for j in xrange(dlen))
        fp.write("%s::%s@%s.%s::%d-%d-%d\n" %(dtstr, login, dom, choice(tlds), dtint, llen, dlen))
```

> gendata.py 在书中 P34 最上


### 1-17
```
import re

def statistics(filename):
    weeks = {}
    with open(filename, 'r') as fp:
        fp_lines = fp.readlines()
        for line in fp_lines:
            res = re.search(r'\w{3}\s\w{3}', line)
            res = res.group().split(' ')[0]
            try:
                weeks[res] += 1
            except:
                weeks[res] = 1
    return weeks

print statistics('redata.txt')
```

### 1-18
> 本书翻译实在是读不通,找到了原文后,自行翻译如下
> 通过确认整数字段的第一个整数与每个输出行开头给出的时间戳匹配,确保redata.txt中没有数据损坏

```
import re
import time

def check_corruption(filename):
    with open(filename, 'r') as fp:
        fp_lines = fp.readlines()
        for line in fp_lines:
            res = re.search(r'(.*?)::[\w@\.]+::(.*?)-', line)
            #print res.group(1), res.group(2)
            if not (time.ctime(int(res.group(2))) == res.group(1)):
                print "check error"

check_corruption('redata.txt')
```
> 由于书上给出的时间是基于美国时间,同时默认参考我答案的人都是中国人,因此如果不是代码运行出来的结果,而是复制书上的代码,那就存在检查失败的风险。请读者一定注意

### 1-19
(.*?)::[\w@\.]+::

### 1-20
::(.*)::

### 1-21
\s(\w{3})\s

### 1-22
(\d{4})::

### 1-23
\d{2}:\d{2}:\d{2}

### 1-24
(\w+)@([\w.]+)::

### 1-25
(\w+)@(\w+)\.(\w{3})

### 1-26
```
import re

def replace_mail(filename, mail):
    with open(filename, 'r') as fp:
        fp_lines = fp.readlines()
        for line in fp_lines:
            res = re.sub(r'\w+@\w+\.\w{3}', mail, line)
            print res

replace_mail('redata.txt', 'username@domain')
```

### 1-27
```
import re

def output_date(filename):
    with open(filename, 'r') as fp:
        fp_lines = fp.readlines()
        for line in fp_lines:
            res = re.search(r'\s(\w{3})\s\s?(\d{1,2})\s.*?\s(\d{4})', line)
            print ', '.join(res.groups())

output_date('redata.txt')
```

### 1-28
(\d{3}-)?\d{3}-\d{3}

### 1-29
(\d{3}-|\(\d{3}\))?\d{3}-\d{4}

### 1-30 1-31 1-32
应用题题干都看不明白,就不在这里献丑了

猜你喜欢

转载自blog.csdn.net/panfengzjz/article/details/81189227