【问题标题】:To Filter String and Only get the Integers [duplicate]过滤字符串并仅获取整数 [重复]
【发布时间】:2021-11-07 08:15:15
【问题描述】:

我正在使用 PYTHON3 我的输出是这样的“200:就在手边”我只需要 200 有没有办法删除字符串和符号?谢谢

【问题讨论】:

  • 您是否尝试过任何无法按预期工作的方法?

标签: python string filter


【解决方案1】:

您可以使用正则表达式删除任何不是数字的内容

>>> import re
>>> s = "200:Just be close at Hand"
>>> re.sub(r'[^\d]', "", s)
'200'
>>> s = "200:Just be close 33 at Hand 342"
>>> re.sub(r'[^\d]', "", s)
'20033342'

【讨论】:

    【解决方案2】:
    >>> txt = "200:Just be close at Hand"
    >>> txt.split(":")[0]
    '200'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-09
      • 2021-10-03
      • 2015-01-26
      • 1970-01-01
      相关资源
      最近更新 更多