V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
k1z
V2EX  ›  Python

Python strip 疑问

  •  
  •   k1z · 235 天前 · 1188 次点击
    这是一个创建于 235 天前的主题,其中的信息可能已经有所发展或是发生改变。
    >>> 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

    有没有大佬解释一下的?

    16 条回复    2023-09-08 09:22:29 +08:00
    raysonx
        1
    raysonx  
       235 天前
    strip('.gz')的意思是去除. g z 三个字符中的任意一个,当年我初学 python 就踩过这坑。
    你的目的可以用 removeSuffix
    Kinnice
        2
    Kinnice  
       235 天前
    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
        3
    k2wang  
       235 天前
    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
        4
    euph  
       235 天前 via Android
    Syntax
    string.strip(characters)

    Parameter Values
    Parameter Description
    characters Optional. A set of characters to remove as leading/trailing characters
    bruce0
        5
    bruce0  
       235 天前
    https://www.v2ex.com/t/969283 前两天 V2 刚讨论过一个 GO 的类似问题
    craiiz
        6
    craiiz  
       235 天前
    哈哈,看见有人踩自己踩过的坑真的好开心
    复制于 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"
    常见新手坑
    k1z
        7
    k1z  
    OP
       235 天前
    @raysonx 那去除有顺序吗? 从前还是从后, 还是说只要末尾是这三个字符中的任意一个,那就一直去除下去
    Rache1
        8
    Rache1  
       235 天前
    同理,PHP 中的 trim 也是如此
    k1z
        9
    k1z  
    OP
       235 天前
    @Kinnice 理解了大佬们
    @k2wang 理解了大佬们
    @euph 理解了大佬们
    @craiiz 理解了大佬们
    hertzry
        10
    hertzry  
       235 天前
    可以试试 print(a.replace('.gz', ''))。
    k1z
        11
    k1z  
    OP
       235 天前
    @hertzry 嗯。 已经用这个方法了
    Azone
        12
    Azone  
       235 天前
    为什么不用 os.path.splitext ?
    k1z
        13
    k1z  
    OP
       234 天前
    @Azone 没用过 - -
    julyclyde
        14
    julyclyde  
       234 天前
    @hertzry replace 不能确保是开头结尾的位置,也可能是中间
    Azone
        15
    Azone  
       234 天前
    @k1z 你不是想去除文件名的扩展名吗?`os.path.splitext('chengyucdntest.okeygame.com_2023090613_5_120.log.gz')[0]` 不能满足你的需求吗?
    k1z
        16
    k1z  
    OP
       233 天前
    @Azone 可以。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3130 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 13:19 · PVG 21:19 · LAX 06:19 · JFK 09:19
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.