V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
kkhu2004
V2EX  ›  问与答

Python 的 strip()有坑啊!你们谁遇到了?

  •  2
     
  •   kkhu2004 · 2018-06-04 09:59:22 +08:00 · 4426 次点击
    这是一个创建于 2124 天前的主题,其中的信息可能已经有所发展或是发生改变。

    s="s,fadsafdsafasf"

    print(s.strip("s,"))

    输出结果是预计的 fadsafdsafasf

    但是!但是!

    print(s.strip("s,f"))

    输出结果居然是 adsafdsafa

    第 1 条附言  ·  2018-06-04 10:37:59 +08:00
    多谢大家。看样子把 strip() 过滤的看出字符串是错误的。
    不能把过滤字符串看成一个整体,它是一个一个字符过滤的。

    具体内容中午休息详细找找。
    第 2 条附言  ·  2018-06-04 11:25:57 +08:00
    The outermost leading and trailing chars argument values are stripped from the string. Characters are removed from the leading end until reaching a string character that is not contained in the set of characters in chars. A similar action takes place on the trailing end. For example:
    >>>

    >>> comment_string = '#....... Section 3.2.1 Issue #32 .......'
    >>> comment_string.strip('.#! ')
    'Section 3.2.1 Issue #32'

    看了文档,大概明白了。strip 是把过滤的字符串拆成 字符,然后挨个过滤首尾的这些字符。
    把它理解成过滤首尾字符串是错误的。
    21 条回复    2021-02-24 17:59:59 +08:00
    easylee
        1
    easylee  
       2018-06-04 10:08:27 +08:00   ❤️ 1
    给你个例子,你参考一下:

    ![]( https://i.loli.net/2018/06/04/5b149edc12bd3.png)

    图截取自“菜鸟教程"。
    ThirdFlame
        2
    ThirdFlame  
       2018-06-04 10:09:39 +08:00   ❤️ 1
    >>> print(s.strip("s,"))
    fadsafdsafasf
    >>> print(s.strip("s,f"))
    adsafdsafa
    >>> print(s.strip("fs,"))
    adsafdsafa
    >>>

    显然是 1 个字符 1 个字符处理的
    aheadlead
        3
    aheadlead  
       2018-06-04 10:11:33 +08:00   ❤️ 1
    RTFM
    virusdefender
        4
    virusdefender  
       2018-06-04 10:11:39 +08:00   ❤️ 1
    str.strip([chars])
    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:

    >>>
    >>> ' spacious '.strip()
    'spacious'
    >>> 'www.example.com'.strip('cmowz.')
    'example'
    loryyang
        5
    loryyang  
       2018-06-04 10:11:54 +08:00   ❤️ 1
    去读下 API,这是你有坑,兄弟
    不要经常以为你有了大发现,人人都在用的东西,哪有那么容易错,先自我检查,是不是自己蠢了
    aheadlead
        6
    aheadlead  
       2018-06-04 10:12:16 +08:00   ❤️ 1
    polaa
        7
    polaa  
       2018-06-04 10:12:16 +08:00   ❤️ 1
    。。。 我觉得没问题,strip return 不就是这么 return 的么
    eurokingbai2
        8
    eurokingbai2  
       2018-06-04 10:13:25 +08:00   ❤️ 1
    没明白楼主想要什么字符串,python 这输出不对么?
    shuax
        9
    shuax  
       2018-06-04 10:21:51 +08:00   ❤️ 1
    RTFM
    liuxey
        10
    liuxey  
       2018-06-04 10:24:41 +08:00   ❤️ 1
    语言内置的基础 API 要是有问题那相关的帖子和新闻早就爆出来了

    我举个不恰当的例子:如果你访问不了百度,你觉得是百度挂了还是你网络连接有问题
    Troevil
        11
    Troevil  
       2018-06-04 10:29:14 +08:00   ❤️ 1
    strip 和 trim 还是有区别的
    dassh
        12
    dassh  
       2018-06-04 10:32:36 +08:00   ❤️ 1
    >>> 'abcde'.strip('ba')
    'cde'
    kkhu2004
        13
    kkhu2004  
    OP
       2018-06-04 10:53:14 +08:00
    @ThirdFlame
    @virusdefender
    手动再次感谢,:)
    kkhu2004
        14
    kkhu2004  
    OP
       2018-06-04 11:06:37 +08:00
    @aheadlead
    @loryyang
    @polaa
    @shuax
    @Troevil

    多谢,初学,只是练习题。是我理解有误。用 strip 直接过滤字符串不合适。
    aheadlead
        15
    aheadlead  
       2018-06-04 11:10:04 +08:00   ❤️ 1
    @kkhu2004 #14 用 s.replace("s,f", "")
    recall704
        16
    recall704  
       2018-06-04 13:23:59 +08:00
    @aheadlead 那如果我只想去掉首尾呢?
    aheadlead
        17
    aheadlead  
       2018-06-04 13:29:36 +08:00
    @recall704 #16
    $ python3
    Python 3.5.2 (default, Nov 23 2017, 16:37:01)
    [GCC 5.4.0 20160609] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> s = '12345'
    >>> s = s[1:-1]
    >>> print(s)
    234
    >>>
    kkhu2004
        18
    kkhu2004  
    OP
       2018-06-04 13:30:11 +08:00
    @recall704 暂时不知道,目前刚刚学,只练习到了这地步。
    楼上( 15 楼)的方法是替换所有的相关字符串,不适用。
    mulog
        19
    mulog  
       2018-06-05 05:25:31 +08:00   ❤️ 1
    >>> s="s,fadsafdsafasf"
    >>> target = 's,f'
    >>> print(s[len(target) if s.startswith(target) else 0: len(s) - len(target) if s.endswith(target) else len(s)])
    adsafdsafasf
    thinker3
        20
    thinker3  
       2018-06-05 09:40:37 +08:00   ❤️ 1
    Pending
        21
    Pending  
       2021-02-24 17:59:59 +08:00   ❤️ 1
    这个坑害我不浅,`'WKHS.US.TXT'.rstrip('.US.TXT')` 将返回 `WKH` 而不是预期的 `WKHS`。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1541 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 17:02 · PVG 01:02 · LAX 10:02 · JFK 13:02
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.