发布于2020-04-09 18:50 阅读(2172) 评论(0) 点赞(29) 收藏(2)
”在举国上下万众一心、众志成城做好新冠肺炎疫情防控工作的特殊时刻,我们不能亲临主战场,但我们能坚持在大战中坚定信心、不负韶华。“
数据库表结构:
代码部分:
import pymysql
import re
import sys,urllib,json
from urllib import request
from datetime import datetime
import pandas as pd
Today=datetime.now().strftime(r"%Y-%m-%d")
#Today='2020-02-14'
def pachong():
url='http://api.tianapi.com/txapi/ncov/index?key=xxx&date={}'.format(Today)
req = request.Request(url)
resp = request.urlopen(req)
content = resp.read().decode()
data=json.loads(content)
with open('/Users/zhangyuchen/Desktop/latestTrends.json','w') as fp:#将所得的数据存储为json文件
json.dump(data,fp = fp,ensure_ascii = False,indent = 4,sort_keys=True)
#dump函数有很多参数,第一个是目标object,第二个是要写入的文件对象
print("成功保存为json文件!")
return(re.findall(r'"confirmedCount":(.+?),"',content),re.findall(r'"currentConfirmedCount":(.+?),"',content),re.findall(r'"curedCount":(.+?),"',content))
def connectMysql(cc):
#/usr/local/mysql/bin/mysql -u root -p
db = pymysql.connect("localhost", "root", "密码", "dbname",charset='utf8' )
cursor = db.cursor()
sql="""insert into {0} (DATE,SICK,SICK_NOW,RECOVER)values('{1}','{2}','{3}','{4}')"""
cursor.execute(sql.format('db1',Today,int(cc[0][0]),int(cc[1][0]),int(cc[2][0])))
cursor.execute(sql.format('db2',Today,int(cc[0][1]),int(cc[1][1]),int(cc[2][1])))
db.commit()
print(("成功将{}数据存入数据库!").format(Today))
db.close()
cc=pachong()
connectMysql(cc)
json文件:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
import pymysql
import re
import sys, urllib,json
from urllib import request
#/usr/local/mysql/bin/mysql -u root -p
date=[]
cSick=[]
aSick=[]
cNowSick=[]
aNowSick=[]
cRecover=[]
aRecover=[]
db = pymysql.connect("localhost", "root", "密码", "trends")
sql="select * from db1 ORDER BY DATE"
cursor = db.cursor()
cursor.execute(sql)
results = cursor.fetchall()
while results:
for row in results:
date.append(row[0].strftime("%d"))
cSick.append(row[1])
cNowSick.append(row[2])
cRecover.append(row[3])
results=cursor.fetchone()
#查询Abroad Table
sql="select * from db2"
cursor.execute(sql)
results = cursor.fetchall()
while results:
for row in results:
aSick.append(row[1])
aNowSick.append(row[2])
aRecover.append(row[3])
results=cursor.fetchone()
cursor.close()
db.close()
def DrawLineChart(ySick,yNowSick):
plt.plot(x,ySick,color='y',label="Cumulative number of cases",linewidth=3,linestyle="--")
plt.plot(x,yNowSick,color='r',label="Current number of cases",linewidth=3,linestyle="-")
def DrawBarChart(yRecover):
width=0.45#柱子宽度
p2 = plt.bar(x,yRecover,width,label="Cured Count",color="#87CEFA")
Days=len(aSick)
plt.figure(figsize=(16,12), dpi=80)#设置分辨率为80像素/每英寸
x=np.arange(Days)
#创建两个子图
plt.subplot(322)
plt.title("Trends of March")
DrawLineChart(cSick,cNowSick)
DrawBarChart(cRecover)
plt.figlegend()
plt.xticks(x,date)
plt.ylabel('Number')
plt.subplot(324)
#plt.title("Trends of March")
DrawLineChart(aSick,aNowSick)
DrawBarChart(aRecover)
plt.xticks(x,date,rotation=0)
plt.xlabel('Date')
plt.ylabel('Number')
plt.show()
我们相信:待凛冬离去,雪融草青,一定有新的相逢将温暖延续。武汉加油!!
原文链接:https://blog.csdn.net/weixin_43959421/article/details/103981713
作者:飞龙出海
链接:https://www.pythonheidong.com/blog/article/319667/58855813d0bfbdc3234b/
来源:python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!