【问题标题】:Remove all non-numeric part but keep the coma in python [duplicate]删除所有非数字部分,但在 python 中保留逗号 [重复]
【发布时间】:2019-04-03 09:03:00
【问题描述】:

假设我有一个字符串,例如:

line = "tree length for XI:      31.0215"

我想只保留31.0215 部分。 我试过了:

print (re.sub("^[0-9]", "",line))

但它不起作用,“。”已删除,有人知道吗?

【问题讨论】:

标签: python


【解决方案1】:

你可以试试这个。

line = "tree length for XI:      31.0215"
for item in line.split():
    try:
        float(item)
        print(float(item))
    except:
        pass

【讨论】:

  • 好的,非常感谢:)
【解决方案2】:
result = ''.join([i for i in line if (i.isdigit() or (i == "."))])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-09
    • 2019-07-31
    • 1970-01-01
    • 2014-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多