python功能代码_整理几个常用的Python功能代码片段【收藏】
隨機數(shù)生成
>>> import random? #導入Python內置的隨機模塊
>>> num = random.randint(1,1000)? #生成1-1000之間的偽隨機數(shù)
讀文件
>>> f = open("c:1.txt","r")
>>> lines = f.readlines() #讀出所有內容給變量 f
>>> for line in lines: # 用循環(huán)輸出每一行
>>>??? print line? #輸出結果
寫文件
>>> f = open("c:1.txt","r+")#可讀可寫模式
>>> f.write("123")#寫入字符串
正則表達式,讀取web服務器的日志并打印日期
>>> import re #正則表達式模塊
>>> regx = "dddd-dd-d+"? # 匹配數(shù)字規(guī)則
>>> f = open("c:stdout.log","r")
>>> i = 0
>>> for str in f.readlines():
>>>??? if re.search(regx,str):
>>>???????? Response.write(str+"
")
>>>???????? if i>10:
>>>??????????? break? #大于10行,自動跳出循環(huán)
>>>????????? i=i+1
>>> f.close();
>>> import pgdb
>>> conn = pgdb.connect (host='localhost',databse='qingfeng',user='qingfeng',password='123')
>>> cur = conn.cursor()
>>> cur.execute("select * from dream")
>>> print cur.rowcount
python:常用功能之文本處理
基礎
在python中,使用str對象來保存字符串。str對象的建立很簡單,使用單引號或雙引號或3個單引號即可。例如:
s='nice'????? #output: nice
s="nice"????? #output: nice
s="Let's go"? #output: Let's go
s='"nice"'??? #output: "nice"
s=str(1)????? #output: 1
s='''''nice
day'''??????? #output: nice
#output: day
在python中,n代表換行符,t代表tab鍵
在python中,引用str中的某一段的內容很容易。例如:
s='123456789'
s[0]????????? #第一個字符:???? output: 1
s[-1]???????? #倒數(shù)第一個字符: output: 9
s[:2]???????? #前2個字符:???? output: 12
s[-2:]??????? #后2個字符:???? output: 89
s[2:-2]?????? #去掉前2個和后2個剩余的字符 output:34567
在python中,判斷某一字符串是否在另一個字符串中:
task 1. 按照某種格式生產(chǎn)字符串
在python中,str對象有一個方法用于實現(xiàn)這種功能,這個方法是:str.format(*args,**kwargs)。例子:
'1+2={0}'.format(1+2)? #{0}是占位符,其中0表示是第一個需要被替換的。output: 1+2=3
'{0}:{1}'.format('nice','day') #{0},{1}是占位符,{0}指第一被替換,替換成nice,{1}第二個被替換,替換成day。output:nice:day
實際用途:
我的手機拍照之后,手機的命名如下:
IMG_20130812_145732.jpg
IMG_20130812_144559.jpg
在電腦中,會根據(jù)相片的日期放到不同的文件夾,文件夾命名如下:
2013-08-10
2013-08-12
所以說,要對相片的命名進行一個轉換,這樣才能映射到相應的文件夾轉。代碼如下:
def getName(name):
return '{0}-{1}-{2}'.format(name[4:8],name[8:10],name[10:12])
getName('IMG_20130812_145732.jpg')? #output: 2013-08-12
task 2. 替換字符串中的某一部分
替換有2中方法,一種是使用str對象自帶的方法replace(),另一種是使用re模塊中sub(0的。例如:
#replace
s='nice day'
s.replace('nice','good')??? #s本身不改變,但會返回一個字符串:output: good day
#sub
import re
s='cat1 cat2 cat3 in the xxx'
re.sub('cat[0-9]','CAT',s)? #s本身不改變,但會返回一個字符串:output: CAT CAT CAT in the xxx
對于re模塊中的sub,需要了解正則表達式。
task 3. 拆分字符串
Excel可以到處逗號分隔符格式的文件。對于這樣的字符串,我們可以把它拆成相應的字段。實現(xiàn)這個功能,主要使用str對象的自帶方法split。例如:
s='one,two,three'
s.split(',')??????????????????????????? #output: ['one', 'two', 'three']
task 4. 合并字符串
除了拆分功能之外,我們可以將拆分好的字段合并成一個字符串。實現(xiàn)這個功能,主要使用str對象自帶的方法join。例如:
l=['one', 'two', 'three']
','.join(l)??????????????????????????????? #output: one,two,three
這個功能還可以在this模塊中看到。
task 5. 整合
關于字符串的操作有很多。如果僅僅對一兩行字符串進行操作,顯示不出他的威力。在工作中,有可能會對文檔進行處理,有的文檔很大,手工的方式不好處理,這時,python就有用武之地。
例如,從數(shù)據(jù)庫中導出一個表table_1的數(shù)據(jù),導出來的數(shù)據(jù)格式如下:
insert into table_1(field1,filed2,field3)
values(value1,value2,value3);
...
insert into table_1(field1,filed2,field3)
values(value1,value2,value3);
數(shù)據(jù)生成的文件的大小大概為700M。要把這個表的數(shù)據(jù)導入到另一個數(shù)據(jù)庫的表table_2中,table_1和table_2的表結構相同,僅僅是名字不同。這時,我們可以寫一個python腳本,將table_1替換成table_2。例如:
path_in='table1.data'
path_out='table2.data'
f_in=open(path_in)
f_out=open(path_out,'w')
for i in f_in.readlines():
if 'insert into table_1(field1,filed2,field3)' in i:
f_out.write(i.repalce('tabel_1','table_2'))
else:
f_out.write(i)
f_in.close()
f_out.close()
總結
以上是生活随笔為你收集整理的python功能代码_整理几个常用的Python功能代码片段【收藏】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python数据类型特点_Python
- 下一篇: python 天气预报地图_在树莓派上用