Python strip 疑问 - V2EX
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 2023-09-06 17:49:30 +08:00 1778 次点击
    这是一个创建于 770 天前的主题,其中的信息可能已经有所发展或是发生改变。
    >>> 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  
       2023-09-06 17:52:24 +08:00
    strip('.gz')的意思是去除. g z 三个字符中的任意一个,当年我初学 python 就踩过这坑。
    你的目的可以用 removeSuffix
    Kinnice
        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"
    常见新手坑
    k2wang
        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 characers 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  
       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
    bruce0
        5
    bruce0  
       2023-09-06 17:57:32 +08:00
    t/969283 前两天 V2 刚讨论过一个 GO 的类似问题
    craiiz
        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"
    常见新手坑
    k1z
        7
    k1z  
    OP
       2023-09-06 18:00:28 +08:00
    @raysonx 那去除有顺序吗? 从前还是从后, 还是说只要末尾是这三个字符中的任意一个,那就一直去除下去
    Rache1
        8
    Rache1  
       2023-09-06 18:00:43 +08:00
    同理,PHP 中的 trim 也是如此
    k1z
        9
    k1z  
    OP
       2023-09-06 18:06:06 +08:00
    @Kinnice 理解了大佬们
    @k2wang 理解了大佬们
    @euph 理解了大佬们
    @craiiz 理解了大佬们
    hertzry
        10
    hertzry  
       2023-09-06 18:25:00 +08:00
    可以试试 print(a.replace('.gz', ''))。
    k1z
        11
    k1z  
    OP
       2023-09-06 18:25:23 +08:00
    @hertzry 嗯。 已经用这个方法了
    Azone
        12
    Azone  
       2023-09-06 18:42:17 +08:00
    为什么不用 os.path.splitext ?
    k1z
        13
    k1z  
    OP
       2023-09-07 09:25:16 +08:00
    @Azone 没用过 - -
    julyclyde
        14
    julyclyde  
       2023-09-07 14:58:05 +08:00
    @hertzry replace 不能确保是开头结尾的位置,也可能是中间
    Azone
        15
    Azone  
       2023-09-07 18:31:09 +08:00
    @k1z 你不是想去除文件名的扩展名吗?`os.path.splitext('chengyucdntest.okeygame.com_2023090613_5_120.log.gz')[0]` 不能满足你的需求吗?
    k1z
        16
    k1z  
    OP
       2023-09-08 09:22:29 +08:00
    @Azone 可以。
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     1070 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 24ms UTC 18:11 PVG 02:11 LAX 11:11 JFK 14:11
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86