a = 'test 123 dfg test'## 方法1len([i for i in a.split(' ') if i == test])## 方法2len(a.split('test'))-1
import re str = '''/begin MEASUREMENT100LINKDISPLAYSYMBOL/end MEASUREMENT''' regex = r'/begin MEASUREMENT([\s\S]*)/end MEASUREMENT'matches = re.findall(regex, str)for match in matches: print(match)
import re str = 'test:100 end' regex = r'test:([\s\S]*)/end'matches = re.findall(regex, str)test = matches[0].strip()
s = ' 123abcd456 '# 刪除兩邊的空格print(s.strip())# 刪除右邊空格print(s.rstrip()) # 刪除左邊空格print(s.lstrip())# 刪除兩邊的數(shù)字print(s.strip(' ').strip('123456'))# 刪除兩邊的引號(hào)s = "'123abcd456'"print(s.strip("'"))
分割并去除空格
string = " hello , world !"string = [x.strip() for x in string.split(',')]
string = "dst='192.168.0.1',src='192.168.1.2'"fields = dict((field.split('=') for field in string.split(',')))fields = dict(((lambda a:(a[0].strip("'"),a[1].strip("'"))) (field.split('=')) for field in string.split(',')))
>>> fields{'dst': "'192.168.0.1'", 'src': "'192.168.1.2'"}
s = '11233aabcdd41556'# 刪除某個(gè)特定字符print(ss.replace('1', ''))# 同時(shí)刪除不同字符import reprint(re.sub('[1a]', '', s))
聯(lián)系客服