日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網營銷解決方案
Python字符串處理的8招秘籍

 Python的字符串處理,在爬蟲的數據解析、大數據的文本清洗,以及普通文件處理等方面應用非常廣泛,而且Python對字符串的處理內置了很多高效的函數,功能非常強大、使用非常方便。今天我就把字符串處理時用到最多的方法總結分享給大家,希望大家可以輕松應對字符串處理。

創(chuàng)新互聯(lián)建站是一家專業(yè)提供含山企業(yè)網站建設,專注與網站建設、成都網站建設、H5開發(fā)、小程序制作等業(yè)務。10年已為含山眾多企業(yè)、政府機構等服務。創(chuàng)新互聯(lián)專業(yè)網站設計公司優(yōu)惠進行中。

1.字符串的切片和相乘

(1)切片

 
 
 
  1. str='Monday is a busy day' 
  2. print(str[0:7])  #表示取第一個到第七個的字符串 
  3. print(str[-3:])  #表示取從倒數第三個字符開始到結尾的字符串 
  4. print(str[::])   #復制字符串 

(2)相乘

當我們編寫Python代碼時要分隔符,此時用字符串的乘法操作就很容易實現(xiàn)。

 
 
 
  1. line='*'*30 
  2. print(line) 
  3. >>****************************** 

2.字符串的分割

(1)普通的分割,用split函數,但是split只能做非常簡單的分割,而且不支持多個分隔。

 
 
 
  1. phone='400-800-800-1234' 
  2. print(phone.split('-')) 
  3. >>['400', '800', '800', '1234'] 

(2)復雜的分割,r表示不轉義,分隔符可以是「;」,或者「,」,或者空格后面跟0個多個額外的空格,然后按照這個模式去分割。

 
 
 
  1. line='hello world; python, I ,like,    it' 
  2. import re 
  3. print(re.split(r'[;,s]\s*',line)) 
  4. >>>['hello world', 'python', 'I ', 'like', 'it'] 

3.字符串的連接和合并

(1)連接,兩個字符可以很方便的通過“+”連接起來

 
 
 
  1. str1='Hello' 
  2. str2='World' 
  3. new_str=str1+str2 
  4. print(new_str) 
  5. >>>HelloWorld 

(2)合并,用join方法

 
 
 
  1. url=['www','python','org'] 
  2. print('.'.join(url)) 
  3. >>>www.python.org 

4.判斷字符串是否以指定前綴、后綴結尾

假設我們要查一個文件的名字是以什么開頭或者什么結尾?

 
 
 
  1. filename='trace.h' 
  2. print(filename.endswith('h')) 
  3. >>True 
  4. print(filename.startswith('trace')) 
  5. >>True 

5.字符串的查找和匹配

(1)一般查找

利用find方法可以很方便的在長的字符串里面查找子字符串,會返回字符串所在位置的索引,若找不到返回-1

 
 
 
  1. str1 = "this is string example....wow!!!" 
  2. str2 = "exam" 
  3. print(str1.find(str2))      # 15 
  4. print(str1.find(str2, 10))  # 15 
  5. print(str1.find(str2, 40))  # -1 

(2)復雜的匹配,就需要用到正則表達式。

 
 
 
  1. mydate='11/27/2016' 
  2. import re 
  3. if re.match(r'\d+/\d+/\d+',mydate): 
  4.     print('ok.match') 
  5. else: 
  6.     print('not match') 
  7.  
  8. >>>ok.match 

6.統(tǒng)計字符串里某個字符出現(xiàn)的次數

 
 
 
  1. str = "thing example....wow!!!" 
  2. print(str.count('i', 0, 5))  # 1 
  3. print(str.count('e'))  # 2 

7.字符串的替換

(1)普通的替換,用replace方法就可以了

 
 
 
  1. text='python is an easy to learn,powerful programming language.' 
  2. print(text.replace('learn','study')) 
  3. >>>python is an easy to study,powerful programming language. 

(2)復雜的替換,需要用到re模塊的sub函數

 
 
 
  1. students='Boy 103,girl 105' 
  2. import re 
  3. print(re.sub(r'\d+','100',students)) 
  4. >>>Boy 100,girl 100 

8.去掉字符串中一些特定的字符

(1)去空格,對文本處理的時候比如從文件中讀取一行,然后需要去除每一行的空格、table或者是換行符。

 
 
 
  1. str = ' python str ' 
  2. print(str) 
  3. # 去首尾空格 
  4. print(str.strip()) 
  5. # 去左側空格 
  6. print(str.lstrip()) 
  7. # 去右側空格 
  8. print(str.rstrip()) 

(2)復雜的文本清理,可以利用str.translate。

比如先構建一個轉換表,table是一個翻譯表,表示把“to”轉成大寫的“TO”,然后在old_str里面去掉‘12345’,然后剩下的字符串再經過table翻譯。

 
 
 
  1. instr = 'to' 
  2. outstr = 'TO' 
  3. old_str = 'Hello world , welcome to use Python. 123456' 
  4. remove = '12345' 
  5. table = str.maketrans(instr,outstr,remove) 
  6. new_str = old_str.translate(table) 
  7. print(new_str) 
  8. >>>HellO wOrld , welcOme TO use PyThOn. 6 

總結

平時我們使用Python都是處理一些腳本,其中使用頻率最大的就是字符串的處理方面,因此給大家整理了這些常用的字符串處理時使用的方法,希望對大家有用。


分享標題:Python字符串處理的8招秘籍
分享URL:http://www.5511xx.com/article/cddccie.html