第 2 章 数据类型-----习题
2.7.1 print_unicode.py1 import sys
2 import unicodedata
3
4 def print_unicode_table(word,word2):
5 print("decimal hex chr {0:^30}".format("name"))
6 print("------- ----- --- {0:-<30}".format(""))
7
8 code = ord(" ")
9 end = sys.maxunicode
10
11 while code < end:
12 c = chr(code)
13 name = unicodedata.name(c, "***unknown***")
14 if (word is None or word in name.lower()) and (word2 is None or word2 in name.lower()):
15 print("{0:7} {0:5X} {0:^3} {1}".format(code, name.title()))
16
17 code += 1
18
19
20 word = None
21 word2 = None
22 if len(sys.argv) > 2:
23 if sys.argv[1] in ("-h", "--help"):
24 print("usage:{0} [string] [string]".format(sys.argv[0]))
25 word = 0
26 else:
27 word = sys.argv[1].lower()
28 word2 = sys.argv[2].lower()
29 if word != 0:
30 print_unicode_table(word,word2)
2.7.2 quadratic.py1 import cmath
2 import math
3 import sys
4
5 def get_float(msg,allow_zero):
6 x = None
7 while x is None:
8 try:
9 x = float(input(msg))
10 if not allow_zero and abs(x) < sys.float_info.epsilon:
11 print("zero is not allowed")
12 x = None
13 except ValueError as err:
14 print(err)
15 return x
16
17 print("ax^2 + bx + c = 0")
18 a = get_float("enter a:", False)
19 b = get_float("enter b:", True)
20 c = get_float("enter c:", True)
21 x1 = None
22 x2 = None
23 discriminant = (b**2) - (4 * a * c)
24 if discriminant == 0:
25 x1 = -(b / (2 * a))
26 else:
27 if discriminant > 0:
28 root = math.sqrt(discriminant)
29 else:
30 root = cmath.sqrt(discriminant)
31
32 x1 = (-b + root)/(2 * a)
33 x2 = (-b - root)/(2 * a)
34 if b == 0 and c == 0:
35 equation = ("{0}x^2 = 0 --> "
36 "x = {1}").format(a,x1)
37 elif b == 0:
38 equation = ("{0}x^2 {1:+} = 0 --> "
39 "x = {2}").format(a,c,x1)
40 elif c == 0:
41 equation = ("{0}x^2 {1:+}x = 0 --> "
42 "x = {2}").format(a,b,x1)
43 else:
44 equation = ("{0}x^2 {1:+}x {2:+} = 0 --> "
45 "x = {3}").format(a,b,c,x1)
46
47 if x2 is not None:
48 equation += " or x = {0}".format(x2)
49
50 print(equation)
2.7.3 cvs2html.py1 import xml.sax.saxutils
2
3 def extract_fields(line):
4 fields = []
5 field = ""
6 quote = None
7 for c in line:
8 if c in "\"""":
9 if quote is None:# start of quoted string
10 quote = c
11 elif quote == c: #end of quoted string
12 quote = None
13 else:
14 field += c # other quote inside quoted string
15 continue
16 if quote is None and c == ",": # end of a field
17 fields.append(field)
18 field = ""
19 else:
20 field += c
21 if field:
22 fields.append(field) # adding the last field
23 return fields
24
25 #def escape_html(text):
26 #text = text.replace("&","&")
27 #text = text.replace("<","<")
28 #text = text.replace(">",">")
29 #return text
30
31 def print_start():
32 print("<table border='1'>")
33
34 def print_end():
35 print("</table>")
36
37 def print_line(line,color,maxwidth):
38 print("<tr bgcolor='{0}'>".format(color))
39 fields = extract_fields(line)
40 for field in fields:
41 if not field:
42 print("<td></td>")
43 else:
44 number = field.replace("," ,"")
45 try:
46 x = float(number)
47 print("<td align='right'>{0:d}</td>".format(round(x)))
48 except ValueError:
49 field = field.title()
50 field = field.replace("And","and")
51 if len(field) <= maxwidth:
52 #field = escape_html(field)
53 xml.sax.saxutils.escape(field)
54 else:
55 #field = "{0}...".format(escape_html(field[:maxwidth]))
56 xml.sax.saxutils.escape(field[maxwidth])
57 print("<td>{0}</td>".format(field))
58 print("</tr>")
59
60 def main():
61 maxwidth = 100
62 print_start()
63 count = 0
64 while True:
65 try:
66 line = input()
67 if count == 0:
68 color = "lightgreen"
69 elif count % 2:
70 color = "white"
71 else:
72 color = "lightyellow"
73 print_line(line,color,maxwidth)
74 count += 1
75 except EOFError:
76 break
77 print_end()
78
79 if __name__ == "__main__":
80 main()