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
sickick
V2EX  ›  Python

lstrip()问题

  •  
  •   sickick · 2022-09-15 14:16:32 +08:00 · 1656 次点击
    这是一个创建于 587 天前的主题,其中的信息可能已经有所发展或是发生改变。
    a = "/root/kyApi/wwwroot/transfer/avatar-oPXiz5enjU37NkmY6nwyadv5LeGk.jpeg"
    print(a.lstrip("/root/kyApi/wwwroot/transfer/avatar-"))
    
    >期待的输出结果: oPXiz5enjU37NkmY6nwyadv5LeGk.jpeg
    >实际的输出结果: PXiz5enjU37NkmY6nwyadv5LeGk.jpeg
    

    请问为什么图片文件名最前面的 o 会被去掉呢?

    13 条回复    2022-12-23 10:03:20 +08:00
    awanabe
        1
    awanabe  
       2022-09-15 14:20:24 +08:00
    A set of characters to remove as leading characters
    参数的意义
    wxf666
        2
    wxf666  
       2022-09-15 14:24:22 +08:00
    [文档]( https://docs.python.org/zh-cn/3/library/stdtypes.html?highlight=lstrip#str.lstrip )说:

    > 实际上 chars 参数并非指定单个前缀;而是会移除参数值的所有组合

    因为 'o' in "/root/kyApi/wwwroot/transfer/avatar-",所以也会移除

    > 参见 [str.removeprefix()]( https://docs.python.org/zh-cn/3/library/stdtypes.html?highlight=lstrip#str.removeprefix ) ,该方法将删除单个前缀字符串,而不是全部给定集合中的字符

    你应该想用 str.removeprefix("/root/kyApi/wwwroot/transfer/avatar-")
    Vegetable
        3
    Vegetable  
       2022-09-15 14:24:34 +08:00
    lstrip 的参数是一个字符集,左边左右在这个字符集的字符都会被去掉。
    a = "abcd"

    a.lstrip("cba") // => "d"
    Vegetable
        4
    Vegetable  
       2022-09-15 14:24:55 +08:00
    @Vegetable 左边只要在这个字符集的字符都会被去掉
    neteroster
        5
    neteroster  
       2022-09-15 14:25:03 +08:00 via Android
    这 strip 不是这么用的,比如你 .lstrip("xyz"),那么从字符串头开始只要遇到 x 或者 y 或者 z 就会删掉,直到遇到不是的就停下来
    xJogger
        6
    xJogger  
       2022-09-15 14:27:09 +08:00
    看了楼上的回复,你这里似乎用 replace 好点
    sickick
        7
    sickick  
    OP
       2022-09-15 14:27:28 +08:00
    @awanabe 还是没太明白。。
    sickick
        8
    sickick  
    OP
       2022-09-15 14:28:43 +08:00
    感谢大佬们的回复 明白了
    julyclyde
        9
    julyclyde  
       2022-09-15 16:35:48 +08:00
    主要是例子太特殊了吧
    如果后边再缺几个字,估计你自己已经意识到了
    thinkershare
        10
    thinkershare  
       2022-09-15 16:43:33 +08:00
    @sickick 想要获取路径中的文件名使用系统提供的 API, 不要自己裁剪
    ysc3839
        11
    ysc3839  
       2022-09-15 16:55:20 +08:00
    多看文档,不要想当然觉得。
    另外 Python 3.9 有 str.removeprefix()
    https://docs.python.org/3/library/stdtypes.html#str.removeprefix
    jiayouzl
        12
    jiayouzl  
       2022-11-08 22:32:21 +08:00
    # -*- coding: UTF-8 -*-

    a = '/root/kyApi/wwwroot/transfer/avatar-oPXiz5enjU37NkmY6nwyadv5LeGk.jpeg'
    print(a.replace('/root/kyApi/wwwroot/transfer/avatar-', ''))

    return:oPXiz5enjU37NkmY6nwyadv5LeGk.jpeg
    julyclyde
        13
    julyclyde  
       2022-12-23 10:03:20 +08:00
    @ysc3839 居然从 3.9 才开始有
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3152 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 14:48 · PVG 22:48 · LAX 07:48 · JFK 10:48
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.