一直想做一个统计天气的小工具,现在终于做成了,代码有点乱,希望多多指点。
代码可以举一反三,有想新法的留个言!
代码在最下面,希望大神能优化,最好能做成一个apk,效果肯定会比这个好!
运行的是qpython3
python 在手机上---收集天气的小工具 python 在手机上---收集天气的小工具
下面是代码
<pre>
--coding:utf8;--
qpy:3
qpy:console
print("This is console module")
import time
import os
os.chdir(os.path.dirname(file))
print(''42)
========创建文件========
if os.path.exists(r'天气记录.txt') == True:
f = open('天气记录.txt', 'a')
else:
f = open('天气记录.txt', 'w')
t = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
========加入数据========
def charu():
t = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
txt = ['晴天','小雨','大雨','阵雨','雷雨','多云','阴天','有雾']
print('_'42)
print('请按下面的天气输入:')
print(txt)
print('='42)
if int(t[11:13]) < 12:
while True:
m = input('上午的天气==>')
if m == '' or m not in txt:
print('输入有误!!!')
continue
else:
break
f.write(t + ' 上午' + '--' + m)
print(''42)
print('插入成功!')
print(t + ' 上午' + '--' + m)
else:
while True:
m = input('下午的天气==>')
if m == '' or m not in txt:
print('输入有误!!!')
continue
else:
break
f.write(t + ' 下午' + '--' + m)
print('*'*32)
print('插入成功!')
print(t + ' 下午' + '--' + m)
f.write('\n')
#f.close()
========查询数据========
def chaxun():
with open("天气记录.txt") as f:
data = f.read()
print(''42)
print(data)
def fenxi():
lines = []
count = {}
sw = {}
xw = {}
qt = {}
o1 = 0
o2 = 0
o3 = 0
o4 = 0
o5 = 0
o6 = 0
o7 = 0
o8 = 0
p1 = 0
p2 = 0
i1 = 0
i2 = 0
u1 = 0
u2 = 0
y1 = 0
y2 = 0
while True:
yue = input('输入要分析的月份[不输入取本月]==>>')
if yue.isdigit():
print(yue,'月')
break
elif yue == '':
t = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
yue = t[5:7]
print(yue,'月')
break
else:
continue
with open("天气记录.txt",'r') as f:
for line in f.readlines():
#line.strip('\n')去掉换行符
lines.append(line.strip('\n'))
#print(type(lines))
#print(len(lines))
#print(lines)
#print(line[11:13])
#print(line[-3:-1])
for line in lines:
#上午是晴
if line[5:7] == yue and line[-2:] == '晴天' and line[-6:-4] == '上午':
p1 += 1
#下午是晴
if line[5:7] == yue and line[-2:] == '晴天' and line[-6:-4] == '下午':
p2 += 1
#上午是雨
if line[5:7] == yue and line[-2:]== '小雨' and line[-6:-4] == '上午':
o1 += 1
#下午是雨
if line[5:7] == yue and line[-2:] == '小雨' and line[-6:-4] == '下午':
o2 += 1
#上午是雨
if line[5:7] == yue and line[-2:] == '大雨' and line[-6:-4] == '上午':
o3 += 1
#下午是雨
if line[5:7] == yue and line[-2:] == '大雨' and line[-6:-4] == '下午':
o4 += 1
#上午是雨
if line[5:7] == yue and line[-2:] == '阵雨' and line[-6:-4] == '上午':
o5 += 1
#下午是雨
if line[5:7] == yue and line[-2:] == '阵雨' and line[-6:-4] == '下午':
o6 += 1
#上午是雨
if line[5:7] == yue and line[-3:] == '雷雨' and line[-6:-4] == '上午':
o7 += 1
#下午是雨
if line[5:7] == yue and line[-3:] == '雷雨' and line[-6:-4] == '下午':
o8 += 1
#上午是多云
if line[5:7] == yue and line[-2:] == '多云' and line[-6:-4] == '上午':
i1 += 1
#下午是多云
if line[5:7] == yue and line[-2:] == '多云' and line[-6:-4] == '下午':
i2 += 1
#上午是阴天
if line[5:7] == yue and line[-2:] == '阴天' and line[-6:-4] == '上午':
u1 += 1
#下午是阴天
if line[5:7] == yue and line[-2:] == '阴天' and line[-6:-4] == '下午':
u2 += 1
#上午是有雾
if line[5:7] == yue and line[-2:] == '有雾' and line[-6:-4] == '上午':
u1 += 1
#下午是有雾
if line[5:7] == yue and line[-2:] == '有雾' and line[-6:-4] == '下午':
u2 += 1
#print(p1,p2,o1,o2,o3,o4,o5,o6,o7,o8,i1,i2,u1,u2)
xw = {
'下午-晴天':p2,
'下午-小雨':o2,
'下午-大雨':o4,
'下午-阵雨':o6,
'下午-雷雨':o8,
'下午-多云':i2,
'下午-阴天':u2,
'下午-有雾':y2,
}
sw = {
'上午-晴天':p1,
'上午-小雨':o1,
'上午-大雨':o3,
'上午-阵雨':o5,
'上午-雷雨':o7,
'上午-多云':i1,
'上午-阴天':u1,
'上午-有雾':y2,
}
#print(sw,xw)
print('_'*42)
for i,j in sw.items():
print(i,j,end='')
for i in range(0,j):
print('|',end='')
print()
print('_'*42)
for i,j in xw.items():
print(i,j,end='')
for i in range(0,j):
print('|',end='')
print()
========调用模块========
def start():
if i == '1':
charu()
elif i == '2':
chaxun()
elif i == '3':
fenxi()
========start开启宝箱========
text ='''1.插入数据 2.查询数据 3.分析数据'''
print(text)
while True:
print('_'*42)
print('*'*42)
i = input('请选择编号[0显示菜单,4退出]=>')
start()
if i == '4':
f.close()
break
elif i == ' ':
continue
elif i == '0':
print(text)
</pre>
下载地址:
天气收集.py
http://pan.baidu.com/s/1dFgqYvn 密码:a3dy
网友评论