国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

Python 去除字符串中指定字符串

瀏覽:3日期:2022-08-03 17:14:32

1、背景

最近的項目中,再次踩到Python字符串處理的坑,決定把此次解決方案記錄一下,以勿踩坑。

2、遇到坑

原本字符串:大坪英利國際8號樓88-88號重慶漢喬科技有限公司大坪英利國際8號樓去除最左邊的字符串:大坪英利國際8號樓預期結果:88-88號重慶漢喬科技有限公司大坪英利國際8號樓

自然而然,第一個想到的就是lstrip()函數。

Python中lstrip() 方法用于截掉字符串左邊的空格或指定字符。但實際上結果:

lstrip: -88號重慶漢喬科技有限公司大坪英利國際8號樓

3、找到 lstrip() 坑的真相

函數原型:

def lstrip(self, chars=None): # real signature unknown; restored from __doc__ ''' S.lstrip([chars]) -> str Return a copy of the string S with leading whitespace removed. If chars is given and not None, remove characters in chars instead. ''' return ''

看來 lstrip 方法是 比對字符 并去除,而不是簡單的去除最左邊字符串。那好,再驗證一下:

'重慶重慶師范大學'.lstrip('重慶')

結果:

師范大學

那我想簡單的去除字符串中的首個指定字符串,最好不用 lstrip() 了。于是又想到了split 方法 和 replace 方法……

4、解決方案

4.1、方法1 split

函數原型:

def split(self, instring, maxsplit=_MAX_INT, includeSeparators=False): ''' Generator method to split a string using the given expression as a separator. May be called with optional C{maxsplit} argument, to limit the number of splits; and the optional C{includeSeparators} argument (default=C{False}), if the separating matching text should be included in the split results. Example::punc = oneOf(list('.,;:/-!?')) print(list(punc.split('This, this?, this sentence, is badly punctuated!'))) prints:: [’This’, ’ this’, ’’, ’ this sentence’, ’ is badly punctuated’, ’’] ''' splits = 0 last = 0 for t,s,e in self.scanString(instring, maxMatches=maxsplit): yield instring[last:s] if includeSeparators: yield t[0] last = e yield instring[last:]

4.2、方法2 replace

函數原型:

def replace(self, old, new, count=None): ''' For each element in `self`, return a copy of the string with all occurrences of substring `old` replaced by `new`. See also -------- char.replace ''' return asarray(replace(self, old, new, count))

5、案例

5.1、源代碼

# -*- coding: utf-8 -*-'''Author: ZhenYuShaCreateTime: 2020-2-26Info: 去除字符串中 首個指定字符串'''def run(source, key): tmp_ls = source.lstrip(key) tmp_re = source.replace(key, '', 1) tmp_sp = source.split(key, 1)[1] return tmp_ls, tmp_re, tmp_spif __name__ == ’__main__’: tmp_1, tmp_2, tmp_3 = run('大坪英利國際8號樓88-88號重慶漢喬科技有限公司大坪英利國際8號樓', '大坪英利國際8號樓') print('test_1 lstrip:', tmp_1) print('test_1 replace:', tmp_2) print('test_1 split:', tmp_3) tmp_1, tmp_2, tmp_3 = run('重慶重慶師范大學', '重慶') print('test_2 lstrip:', tmp_1) print('test_2 replace:', tmp_2) print('test_2 split:', tmp_3)

5.2、效果

Python 去除字符串中指定字符串

6、延伸

split 和 replace 可以解決字符串首個指定字符串去除問題, 但去除字符串這個問題不僅僅是去除就完了,還要去判斷是否符合我們的要求。

6.1、看字符串開頭是否是指定字符串

如果需要以指定字符串開頭,要用 startswith 函數來判斷。

6.2、看字符串中是否存在指定字符串

如果不存在指定字符串,直接用 split 和 replace 會直接崩潰的,那就需要 find 函數來查看了。

到此這篇關于Python 去除字符串中指定字符串的文章就介紹到這了,更多相關Python 去除字符串 內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 日韩欧美一级毛片精品6 | 久久亚洲国产午夜精品理论片 | 黄色a免费 | 一本到不卡 | 国产v综合v亚洲欧美大另类 | 一区二区三区免费视频 www | 一色屋色费精品视频在线观看 | 国产欧美日韩一区二区三区在线 | 亚洲欧美日本在线观看 | 国产精品99久久久久久www | 九九精品免视看国产成人 | 国产精品久久久久久影视 | 一级黄色免费网站 | 亚洲性生活视频 | 久久99精品久久只有精品 | 国产农村一二三区 | 欧美成人短视频 | 免费a级毛片无码 | 国产日韩精品一区在线观看播放 | 国产成人mv 在线播放 | 国产人妖xxxx做受视频 | 99久在线精品99re6视频 | 国产精品久久久亚洲 | 俄罗斯极品美女毛片免费播放 | 加勒比综合 | 国产精品1页 | 免费一级特黄 欧美大片 | 亚洲视频在线网 | 欧美13一14周岁a在线播放 | 成人午夜大片免费7777 | 大尺度福利视频在线观看网址 | 亚洲精品一级一区二区三区 | 一级aaa级毛片午夜在线播放 | 456主播喷水在线观看 | 欧洲一级大片 | 国产日韩欧美精品 | 亚洲精品理论 | 成人免费一区二区三区视频软件 | 高清不卡一区二区三区 | 无码免费一区二区三区免费播放 | 亚洲视频日韩 |