重慶分公司,新征程啟航
為企業(yè)提供網(wǎng)站建設(shè)、域名注冊、服務(wù)器等服務(wù)
為企業(yè)提供網(wǎng)站建設(shè)、域名注冊、服務(wù)器等服務(wù)
#已知文件名的情況下,可以用這個函數(shù)
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務(wù)項目有:主機(jī)域名、雅安服務(wù)器托管、營銷軟件、網(wǎng)站建設(shè)、南陽網(wǎng)站維護(hù)、網(wǎng)站推廣。
def getFileInfo(filepath):
info = {}
if os.path.isfile(filepath):
info['TimeCreated'] = os.path.getctime(filepath)
info['TimeModified'] = os.path.getatime(filepath)
info['Size'] = os.path.getsize(filepath)
return info
在弄些棧用來臨時存放就成了, 比如說
class?Stack(list):
def?is_empty(self):
return?len(self)?==?0
def?push(self,?data):
self.append(data)
def?rev(s):
def?move(src,?dst):
while?not?src.is_empty():
dst.push(src.pop())
a?=?Stack()
b?=?Stack()
move(s,?a)
move(a,?b)
move(b,?s)
sta?=?Stack()
sta.push(1)
sta.push(2)
sta.push(3)
rev(sta)
print(sta.pop())
print(sta.pop())
print(sta.pop())
結(jié)果是
1
2
3
import scipy.stats as sta
import math
def option_call(s,x,r,sigma,t):
d1=(math.log(s/x)+(r+sigma**2/2)*t)/(math.sqrt(t)*sigma)
d2=d1-sigma*math.sqrt(t)
c=s*sta.norm.cdf(d1,0,1)-x*sta.norm.cdf(d2,0,1)*math.exp(-r*t)
return c
sta是所有ser類對象公有的類變量,在一個對象中對其進(jìn)行修改當(dāng)然會影響到其他對象中sta的內(nèi)容。在構(gòu)造函數(shù)中加上self.sta=[]可將sta變?yōu)槊總€對象獨(dú)立持有的成員變量。
序號 ? ?描述 ?
1 ? ?去github上下載pymysql的安裝包pymysql ?
2 ? ?解壓到某個盤符下 ?
3 ? ?打開cmd窗口(win環(huán)境下),進(jìn)入pymysql的根目錄下執(zhí)行命令,python setup.py install ?
4 ? ?在程序里,導(dǎo)入pymysql ?
5 ? ?開始連接數(shù)據(jù)庫 ?
數(shù)據(jù)庫操作的API文檔連接:?
代碼如下:?
Python代碼
__author__?=?'qindongliang'
#導(dǎo)入pymysql的包
import?pymysql
try:
#獲取一個數(shù)據(jù)庫連接,注意如果是UTF-8類型的,需要制定數(shù)據(jù)庫
conn=pymysql.connect(host='localhost',user='root',passwd='qin',db='person',port=3306,charset='utf8')
cur=conn.cursor()#獲取一個游標(biāo)
cur.execute('select?*?from?person')
data=cur.fetchall()
for?d?in?data?:
#注意int類型需要使用str函數(shù)轉(zhuǎn)義
print("ID:?"+str(d[0])+'??名字:?'+d[1]+"??性別:?"+d[2])
cur.close()#關(guān)閉游標(biāo)
conn.close()#釋放數(shù)據(jù)庫資源
except??Exception?:print("發(fā)生異常")
結(jié)果如下:?
Java代碼
D:\python\python.exe?D:/pythonide/pythonprojectworkspace/python/mysql.py
ID:?1??名字:?秦天??性別:?男
ID:?2??名字:?王晶??性別:?女
Process?finished?with?exit?code?0 ?
原文??
補(bǔ)充說明:
mysqldb作為python連接mysql數(shù)據(jù)庫的工具,但是mysqldb目前支撐的版本較低,安裝失敗。所以才嘗試pymysql,這個比較簡單易用
軟件下載地址:
python3.2.5:
pymysql3.0.5:
mysql:(為了方便安裝,我這里選擇phpstudy)
1、python安裝目錄設(shè)定為d:/python32
2、pymysql安裝方法為:解壓下載的文件,在cmd中運(yùn)行: python setup.py install。
檢驗安裝安裝是否成功的方法:import pymysql? 。? 如果不報錯 說明安裝成功。
3、mysql安裝目錄為D:/phpStudy/MySQL。為避免更多配置問題,可在啟動phpstudy后,將其設(shè)為系統(tǒng)服務(wù)
4、基本操作:
(1)導(dǎo)入pymysql: import pymysql
(2)連接數(shù)據(jù)庫: conn=pymysql.connect(host='localhost',user='root',passwd='root',db='ere',charset='utf8')??? 務(wù)必注意各等號前面的內(nèi)容!charset參數(shù)可避免中文亂碼
(3)獲取操作游標(biāo):cur=conn.cursor()
(4)執(zhí)行sql語句,插入記錄:sta=cur.execute("insert 語句")? 執(zhí)行成功后sta值為1。更新、刪除語句與此類似。
(5)執(zhí)行sql語句,查詢記錄:cur.execute("select語句") 執(zhí)行成功后cur變量中保存了查詢結(jié)果記錄集,然后再用循環(huán)打印結(jié)果:
for each in cur:
print(each[1].decode('utf-8'))???? # each[1] 表示當(dāng)前游標(biāo)所在行的的第2列值,如果是中文則需要處理編碼
#!/usr/bin/python
#?-*-?coding:utf-8?-*-
#?@Time?:?2018/6/18?13:45
#?@File?:?Statistics_String.py
"""
統(tǒng)計字符串中字符出現(xiàn)的個數(shù)
"""
def?stastr(astr):
"""統(tǒng)計字符"""
temp?=?astr.lower()
tset?=?set(temp)
strdict?=?{}
for?ss?in?tset:
num?=?temp.count(ss)
strdict.setdefault(ss,?num)
else:
return?strdict
if?__name__?==?'__main__':
ostr?=?raw_input(u'請輸入一個字符串:')
print?stastr(ostr)