Skip to content

Commit 662884b

Browse files
committed
Create 0020.py
1 parent a27e38d commit 662884b

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

Liez-python-code/0020/0020.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import xlrd
2+
import re
3+
from pylab import *
4+
5+
6+
def get_xls_data(filename):
7+
8+
# 读取xls数据
9+
book = xlrd.open_workbook(filename)
10+
sheet = book.sheet_by_index(0)
11+
content = {}
12+
for i in range(sheet.nrows):
13+
content[i+1] = sheet.row_values(i)[1:]
14+
#print(content[i+1])
15+
16+
print("Liez的3月话费单:")
17+
18+
# 统计话费
19+
cost = 0
20+
for i in range(sheet.nrows-1):
21+
cost += float(content[i+2][7])
22+
print("通话费:", round(cost, 2),"元")
23+
24+
# 统计被叫主叫次数
25+
call = 0
26+
becall = 0
27+
for i in range(sheet.nrows-1):
28+
if(content[i+2][3] == "主叫"):
29+
call += 1
30+
else:
31+
becall += 1
32+
total = call + becall
33+
print("主叫",call,"次,被叫",becall,"次,共计",total,"次通话")
34+
35+
# 统计通话时间
36+
time = {}
37+
for i in range(sheet.nrows-1):
38+
time[i] = content[i+2][1]
39+
day = {}
40+
for i in range(31):
41+
day[i+1] = 0
42+
rday = re.compile('-(\d+?) ')
43+
for i in range(sheet.nrows-1):
44+
daycompile = rday.findall(content[i+2][1])
45+
t = int(daycompile[0])
46+
day[t] += 1
47+
daytimes = (list(day.values()))
48+
dates = (list(day.keys()))
49+
50+
# 统计通话时长
51+
sec = 0
52+
min = 0
53+
for i in range(sheet.nrows-1):
54+
rsec = re.compile('(\d+)秒')
55+
rmin = re.compile('(\d+)分')
56+
seci = rsec.findall(content[i+2][2])
57+
mini = rmin.findall(content[i+2][2])
58+
sec += int(seci[0])
59+
if(len(mini)==1):
60+
min += int(mini[0])
61+
if(sec >= 60):
62+
t = sec / 60
63+
sec = sec % 60
64+
min = min + t
65+
print("通话时长:%d分%d秒"%(min,sec))
66+
67+
#三月日通话次数统计图
68+
plt.plot(dates, daytimes)
69+
grid(True)
70+
title("Call Times Every Day")
71+
plt.show()
72+
73+
#根据被叫主叫次数作饼状图
74+
figure(2, figsize=(6,6))
75+
fracs = [call/total, becall/total] # 饼状图按被叫和主叫分成两部分的比例
76+
labels = 'Call', 'Becall'
77+
pie(fracs, labels=labels, autopct='%1.1f%%', shadow=True, startangle=90, colors = ("b", "y"))
78+
title("Call & Becall")
79+
show()
80+
81+
82+
get_xls_data("comu.xls")

0 commit comments

Comments
 (0)