【问题标题】:How to get number from a string with python [duplicate]如何使用python从字符串中获取数字[重复]
【发布时间】:2019-02-23 03:36:06
【问题描述】:

我想从字符串中获取数字

例子:

a = '22,123 games'

结果:

result = 22123

【问题讨论】:

  • 你的代码在哪里?

标签: python python-3.x


【解决方案1】:

首先你需要找到号码在哪里。 我假设它在第一个空格之前,所以

number = a[0 : a.find(' ')]

然后你必须删除逗号:

noCommas = number.replace(',', '')

然后转成int:

res = int(noCommas)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-04
    • 1970-01-01
    • 2021-03-06
    • 2016-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-23
    相关资源
    最近更新 更多