>>> 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
有没有大佬解释一下的?
1
raysonx 2023-09-06 17:52:24 +08:00
strip('.gz')的意思是去除. g z 三个字符中的任意一个,当年我初学 python 就踩过这坑。
你的目的可以用 removeSuffix |
2
Kinnice 2023-09-06 17:55:00 +08:00
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" 常见新手坑 |
3
k2wang 2023-09-06 17:56:32 +08:00
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 的参数仅表示从开头或结尾移除的字符集 |
4
euph 2023-09-06 17:56:52 +08:00 via Android
Syntax
string.strip(characters) Parameter Values Parameter Description characters Optional. A set of characters to remove as leading/trailing characters |
5
bruce0 2023-09-06 17:57:32 +08:00
https://www.v2ex.com/t/969283 前两天 V2 刚讨论过一个 GO 的类似问题
|
6
craiiz 2023-09-06 17:57:40 +08:00
哈哈,看见有人踩自己踩过的坑真的好开心
复制于 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" 常见新手坑 |
8
Rache1 2023-09-06 18:00:43 +08:00
同理,PHP 中的 trim 也是如此
|
10
hertzry 2023-09-06 18:25:00 +08:00
可以试试 print(a.replace('.gz', ''))。
|
12
Azone 2023-09-06 18:42:17 +08:00
为什么不用 os.path.splitext ?
|