Python核心编程——Chapter9

时间:2021-07-18 22:45:58

好久没写过Python了,前一阵子忙这忙那的,都几乎把Python给丢掉了,话不多说,马上开始。

9.1.文件过滤。显示一个文件的所有行,并且忽略以井号开头的行。

其实这个题目比较基础,用shell语言的话,一行就能搞定。还是做一下吧。

用shell语言+正则:

dzhwen@deng:~/python文件/Homework/Chapter9$ cat regular_express.txt | grep -v '^#'

用python:

 1 #!/usr/bin/env python
 2 #-*-coding=utf-8-*-
 3 
 4 def show(filename):
 5     f = open(filename,'r')
 6     for eachLine in f:
 7         if eachLine[0] != '#':
 8             print eachLine,
 9 
10 if __name__ == '__main__':
11     show('/home/dzhwen/regular_express.txt')

9.2.又是文件访问题目,显示文件F的前N行

shell:

dzhwen@deng:~/python文件/Homework/Chapter9$ head -n N F

python:

 1 #!/usr/bin/env python
 2 #-*-coding=utf-8-*-
 3 
 4 def head():
 5     filename = raw_input('请输入文件名称:')
 6     lines = raw_input('请输入行数:')
 7     start = 0
 8     f = open(filename,'r')
 9     while start < int(lines):
10         print f.next(),
11         start += 1
12 
13 if __name__ == '__main__':
14     head()

9.3.文件信息。显示一个文件的总行数

shell:

dzhwen@deng:~/python文件/Homework/Chapter9$ wc -l regu*
22 regular_express.txt

python:

>>> f = open('/home/dzhwen/regular_express.txt')
>>> list = f.readlines()
>>> len(list)
22

 9.14.将计算器程序修改成接受命令行参数

这道题目还是挺有挑战性的。用到的是sys模块的命令行参数

 1 #!/usr/bin/env python
 2 #-*-coding=utf-8-*-
 3 
 4 import sys
 5 
 6 def calc():
 7     f = open('/home/dzhwen/123.txt','a+')
 8     list = sys.argv
 9     if list[1] == 'print':
10         for eachLine in f:
11             print eachLine,
12         return -1
13     if list[2] == '+':
14         ca = list[1:]
15         for char in ca:
16             f.write(char+' ')
17         f.write('\n')
18         f.write(str(float(list[1]) + float(list[3]))+'\n')
19     if list[2] == '-':
20         ca = list[1:]
21         for char in ca:
22             f.write(char+' ')
23         f.write('\n')
24         f.write(str(float(list[1]) - float(list[3]))+'\n')
25     if list[2] == '*':
26         ca = list[1:]
27         for char in ca:
28             f.write(char+' ')
29         f.write('\n')
30         f.write(str(float(list[1]) * float(list[3]))+'\n')
31     if list[2] == '/':
32         ca = list[1:]
33         for char in ca:
34             f.write(char+' ')
35         f.write('\n')
36         f.write(str(float(list[1]) / float(list[3]))+'\n')
37     if list[2] == '%':
38         ca = list[1:]
39         for char in ca:
40             f.write(char+' ')
41         f.write('\n')
42         f.write(str(float(list[1]) % float(list[3]))+'\n')
43     if list[2] == '^':
44         ca = list[1:]
45         for char in ca:
46             f.write(char+' ')
47         f.write('\n')
48         f.write(str(float(list[1]) ** float(list[3]))+'\n')
49 
50 if __name__ == '__main__':
51     calc()

9.15.复制文件,将第一个文件的内容复制到第二个文件中去。

同时打开两个通道看可不可以。

#!/usr/bin/env python
#-*-coding=utf-8-*-

def copy(file1,file2):
    f = open(file1,'r')
    g = open(file2,'a+')
    for eachLine in f:
        g.write(eachLine)

if __name__ == '__main__':
    copy('/home/dzhwen/123.txt','/home/dzhwen/456.txt')

 9.18.搜索文本。查找一个字符在文中的出现次数。

shell:

dzhwen@deng:~/python文件/Homework/Chapter9$ cat regular_express.txt | grep '#' |wc -l
5

python:没有现成函数,唯有用count

 1 #!/usr/bin/env python
 2 #-*-coding=utf-8-*-
 3 
 4 def charcount(filename):
 5     num = raw_input('请输入字节值:')
 6     Count = 0
 7     f = open(filename,'r')
 8     for eachLine in f:
 9         Count += eachLine.count(chr(int(num)))
10     return Count
11 
12 if __name__ == '__main__':
13     print charcount('/home/dzhwen/regular_express.txt')