Python
(2021-02-11 00:10:31)Below is my study on Python
to mine websites,mssql,excel and stata datasets:
#request website information
code
import requests as
rq
response=rq.get("http://www.baidu.com")
print(response.status_code)
print(response.text)
#connect mssql
import pymssql
conn=pymssql.connect('(local)','sa','123456','CIS')
cursor=conn.cursor()
sql="create table pytest(id
char(10),name char(30),gender char(30))"
cursor.execute(sql)
conn.commit()
sql="insert into
pytest(id,name,gender) values (1000,'jack','male')"
cursor.execute(sql)
conn.commit()
sql="insert into
pytest(id,name,gender) values (1001,'alice','female')"
cursor.execute(sql)
conn.commit()
sql="select * from
pytest"
cursor.execute(sql)
conn.commit()
row=cursor.fetchone()
while row:
sql="update pytest set
id=1003,name='jane' where id=1001"
cursor.execute(sql)
conn.commit()
sql="delete from
pytest"
cursor.execute(sql)
conn.commit()
sql="drop table
pytest"
cursor.execute(sql)
conn.commit()
cursor.close()
conn.close()
#mine excel data
import xlrd
import matplotlib.pyplot as
plt
import numpy as np
xl=xlrd.open_workbook(r'D:\data.xls')
sheet=xl.sheets()[1]
Xs=[]
As=[]
Bs=[]
for i in
range(1,10):
print(Xs,As,Bs)
plt.bar(np.arange(len(Xs)),height=As,width=0.3,color="red")
plt.bar(np.arange(len(Xs))+0.3,height=Bs,width=0.3,color="green")
plt.title('test')
plt.legend('CD')
plt.xlabel('x')
plt.ylabel('y')
plt.show
#connect stata
import ipystata
from ipystata.config import
config_stata
config_stata("C:\Program Files
(x86)\Stata14\StataMP-64.exe")
import pandas as
pd
import ipystata
%%stata
sysuse
auto.dta,clear
sum
pwcorr_a *
graph twoway (scatter price
mpg)(lfit price mpg)(qfit price mpg)
local x "headroom trunk
rep78"
qui:eststo:reg price
`x'
qui:eststo:reg mpg
`x'
esttab,b(%4.3f)
t(%6.2f)
eststo clear
%%stata
*draw world map
use
"C:\Users\john\Desktop\workfiles\quantitative analysis
method\world-d",clear
rename _ID id
spmap using
"C:\Users\john\Desktop\workfiles\quantitative analysis
method\world-c",id(id) fcolor(green)
%%stata
*draw China map
use
"C:\Users\john\Desktop\workfiles\quantitative analysis
method\China_label",clear
spmap using
"C:\Users\john\Desktop\workfiles\quantitative analysis
method\China_map",id(id) fcolor(red)
%%stata
*draw US map
use
"C:\Users\john\Desktop\workfiles\quantitative analysis
method\stats",clear
merge 1:1 scode using
"C:\Users\john\Desktop\workfiles\quantitative analysis
method\trans"
drop _merge
merge 1:1 id using
"C:\Users\john\Desktop\workfiles\quantitative analysis
method\usdb"
drop if _merge!=3
drop _merge
replace
pop1990=pop1990/1e+6
format pop1990
%4.2f
spmap pop1990 using
"C:\Users\john\Desktop\workfiles\quantitative analysis
method\uscoord" if id!=1 & id!=39 & id!=54 &
id!=56,id(id) fcolor(blue)
%%stata
*draw Italy map
use
"C:\Users\john\Desktop\workfiles\quantitative analysis
method\Italy-RegionsData",clear
spmap using
"C:\Users\john\Desktop\workfiles\quantitative analysis
method\Italy-RegionsCoordinates",id(id) fcolor(blue)