python字符串的正則匹配:re模塊
正則表達式在匹配負責字符串的時候,確實很有用:
>>>importre
>>>re.findall(r'\bf[a-z]*','whichfootorhandfellfastest')
['foot','fell','fastest']
>>>re.sub(r'(\b[a-z]+)\1',r'\1','catinthethehat')
'catinthehat'
但是,如果是簡單的對字符串進行操作,官網推薦的還是使用string模塊的函數,因為這些函數看起來更易讀更簡潔,更容易調試:
>>>'teafortoo'.replace('too','two')
'teafortwo'
互聯網訪問:urllib2模塊
用于訪問互聯網以及處理網絡通信協議中,最簡單的兩個是用于處理從urls接收的數據的urllib2以及用于發送電子郵件的smtplib。
最簡單的例子:
importurllib2
response=rllib2.urlopen('http://python.org/')
html=response.read()
日期和時間:datetime模塊
datetime模塊為日期和時間處理同時提供了簡單和復雜的方法。支持日期和時間算法的同時,實現的重點放在更有效的處理和格式化輸出。該模塊還支持時區處理,函數方法比較簡單,就直接從官網截取例子:
>>>#datesareeasilyconstructedandformatted
>>>fromdatetimeimportdate
>>>now=date.today()
>>>now
datetime.date(2003,12,2)
>>>now.strftime("%m-%d-%y.%d%b%Yisa%Aonthe%ddayof%B.")
'12-02-03.02Dec2003isaTuesdayonthe02dayofDecember.'
>>>#datessupportcalendararithmetic
>>>birthday=date(1964,7,31)
>>>age=now-birthday
>>>age.days
14368
以上內容為大家介紹了python字符串的正則匹配:re模塊,希望對大家有所幫助,如果想要了解更多Python相關知識,請關注IT培訓機構:千鋒教育。

相關推薦HOT
更多>>
Python如何輸出為文件
python輸出為文件使用withopen()as語句。使用示例:withopen('E:\python\python\test.txt','w')asf:f.writ...詳情>>
2023-11-09 22:08:08
怎么查看python變量的類型
python判斷變量的類型有兩種方法:type()和isinstance()對于基本的數據類型兩個的效果都一樣type()ip_port=['219.135.164.245',3128...詳情>>
2023-11-09 21:30:21
python如何生成文件夾
python中可以使用os.makedirs()方法創建多級目錄:os.makedirs()方法用于遞歸創建目錄。像mkdir(),但創建的所有intermediate-level文件夾需要包...詳情>>
2023-11-09 21:11:28
怎樣用python計算矩陣乘法?
python中計算矩陣乘法的方法:1、使用np.multiply()函數計算矩陣乘法函數作用:數組和矩陣對應位置相乘,輸出與相乘數組/矩陣的大小一致示例:n...詳情>>
2023-11-09 20:01:23