午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

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

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

1、背景

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

2、遇到坑

原本字符串:大坪英利國(guó)際8號(hào)樓88-88號(hào)重慶漢喬科技有限公司大坪英利國(guó)際8號(hào)樓去除最左邊的字符串:大坪英利國(guó)際8號(hào)樓預(yù)期結(jié)果:88-88號(hào)重慶漢喬科技有限公司大坪英利國(guó)際8號(hào)樓

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

Python中l(wèi)strip() 方法用于截掉字符串左邊的空格或指定字符。但實(shí)際上結(jié)果:

lstrip: -88號(hào)重慶漢喬科技有限公司大坪英利國(guó)際8號(hào)樓

3、找到 lstrip() 坑的真相

函數(shù)原型:

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 ''

看來(lái) lstrip 方法是 比對(duì)字符 并去除,而不是簡(jiǎn)單的去除最左邊字符串。那好,再驗(yàn)證一下:

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

結(jié)果:

師范大學(xué)

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

4、解決方案

4.1、方法1 split

函數(shù)原型:

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

函數(shù)原型:

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: 去除字符串中 首個(gè)指定字符串'''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('大坪英利國(guó)際8號(hào)樓88-88號(hào)重慶漢喬科技有限公司大坪英利國(guó)際8號(hào)樓', '大坪英利國(guó)際8號(hào)樓') 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('重慶重慶師范大學(xué)', '重慶') print('test_2 lstrip:', tmp_1) print('test_2 replace:', tmp_2) print('test_2 split:', tmp_3)

5.2、效果

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

6、延伸

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

6.1、看字符串開(kāi)頭是否是指定字符串

如果需要以指定字符串開(kāi)頭,要用 startswith 函數(shù)來(lái)判斷。

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

如果不存在指定字符串,直接用 split 和 replace 會(huì)直接崩潰的,那就需要 find 函數(shù)來(lái)查看了。

到此這篇關(guān)于Python 去除字符串中指定字符串的文章就介紹到這了,更多相關(guān)Python 去除字符串 內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 日韩视频网 | 五月婷婷婷婷 | 亚洲第一在线视频 | 国产911| 日韩在线欧美 | 国产精品日韩欧美大师 | 色综合久久88 | 中文字幕在线免费看 | 九九国产精品视频 | 日日夜夜艹 | 欧美日韩精品国产 | 一级在线观看视频 | 亚洲精品在线免费 | 黄色一级大片免费版 | 久久这里都是精品 | 欧美 日韩 中文字幕 | 免费av一级片 | 91手机看片| 久久国产精品久久精品国产 | 国产午夜免费视频 | 亚洲第一色网站 | 日韩av一区二区三区在线观看 | 超碰2021 | 私库av在线 | 免费爱爱视频 | 欧美午夜精品久久久久免费视 | 一区二区免费 | 日韩一区二区三区在线播放 | 欧美日韩色综合 | 国产毛片久久久久久久 | 亚洲激情第一页 | 你懂的免费在线观看 | 福利精品 | 欧美日韩在线国产 | 欧美精品另类 | 久久中国 | 成人aaaaa | 亚洲乱码中文字幕 | 久久久久久黄色 | 国外成人性视频免费 | 快灬快灬一下爽蜜桃在线观看 |