Python strip 疑问

查看 94|回复 6
作者:k1z   
>>> a = 'chengyucdntest.okeygame.com_2023090613_5_120.log.gz'
>>> a.strip('.gz')
'chengyucdntest.okeygame.com_2023090613_5_120.lo'
>>> b = 'E384UD843W7GXZ.2023-09-06-09.fce6535e.gz'
>>> b.strip('.gz')
'E384UD843W7GXZ.2023-09-06-09.fce6535e'
如上, 有点无法理解为什么 a.strip('.gz')的值后边会少一个 g
有没有大佬解释一下的?

rip, 2023-09-06, Python

raysonx   
strip('.gz')的意思是去除. g z 三个字符中的任意一个,当年我初学 python 就踩过这坑。
你的目的可以用 removeSuffix
Kinnice   
https://docs.python.org/3/library/stdtypes.html?highlight=strip#str.strip
The chars argument is not a prefix or suffix; rather, all combinations of its values are stripped"
常见新手坑
k2wang   
Python 文档是这么解释的。
Return a copy of the string with the leading and trailing characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a prefix or suffix; rather, all combinations of its values are stripped.
a.strip('gz.')会得到一样的结果,strip 的参数仅表示从开头或结尾移除的字符集
euph   
Syntax
string.strip(characters)
Parameter Values
Parameter Description
characters Optional. A set of characters to remove as leading/trailing characters
bruce0   
/t/969283 前两天 V2 刚讨论过一个 GO 的类似问题
craiiz   
哈哈,看见有人踩自己踩过的坑真的好开心
复制于 2 楼:
https://docs.python.org/3/library/stdtypes.html?highlight=strip#str.strip
The chars argument is not a prefix or suffix; rather, all combinations of its values are stripped"
常见新手坑
您需要登录后才可以回帖 登录 | 立即注册

返回顶部