I want to get numeric data from string. How to do that in python?

Sshantanu Agarwal

I want to get numeric data from string. How to do that in python???

For e.g. From string "data23/45 data" extract 23 and 45

Joachim Isaksson

I'm sure there are hundreds of ways, but one way is to use a regular expression to split the string into digit groups, and a list comprehension to convert the values to integers;

>>> import re
>>> a = "data23/45 data"
>>> [int(x) for x in re.split('[^\d]+', a) if x]
[23, 45]

re.split('[^\d]+', a) will split on non digits, which leaves only the digit groups in the result. The outer list comprehension will convert all non empty values in the result to integers.

Of course, if you want the digit groups still as strings, you can use the simpler;

>>> [x for x in re.split('[^\d]+', a) if x]
['23', '45']

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

How do I get the length of the first line in a multi line string?

来自分类Dev

How do I find out the numeric ID of the current Postgres Transaction?

来自分类Dev

How do I get a value from an associative array using Swift

来自分类Dev

How to get type from String?

来自分类Dev

How do I get all the values of a String array to use in a return statement in a method?

来自分类Dev

In JIRA Agile, when I click a quickfilter I want other quickfilters to be unclicked, how can I do this?

来自分类Dev

libevent evhttp how do I get the body length or indeed any body data?

来自分类Dev

Fast and safe conversion from string to numeric types

来自分类Dev

How do I get a Cookie from a SoapUI response using a Groovy test step?

来自分类Dev

How do I mock a method call inside the method I want to test using NUnit?

来自分类Dev

In my haml file, I want to include a link within a string that is coming from a yml file

来自分类Dev

Distinguish between 'character' and 'numeric' data types in Python

来自分类Dev

How can I send data from a TCP Python server to a client and vice-versa?

来自分类Dev

How do I catch all of these exceptions in python?

来自分类Dev

I want to extract strings from a line

来自分类Dev

how do retrieve data from core data table ios?

来自分类Dev

How do I copy data from each record of one table into multiple records of another table related to the first table?

来自分类Dev

How do I assign contents of a string to a text field in Unity 4.6?

来自分类Dev

How do I get the nth derivative in my scheme program?

来自分类Dev

Swift: How do I get access to the navigationController in AppDelegate

来自分类Dev

How do I get the Area (or the path) of where a View is being scaffolded?

来自分类Dev

How do I get my aliases to have correct completion?

来自分类Dev

How do i get MS EXCEL to Connect to MYSQL Database

来自分类Dev

How do I get MVC to find my controller path?

来自分类Dev

How do I get Avg and Sum in Spark RDD

来自分类Dev

How do I get my nested if statement to work in jQuery

来自分类Dev

How do I get Ruby to search for a pattern on the tail of a local file?

来自分类Dev

How do I get the computer's current language in Go?

来自分类Dev

How do I efficiently crop an *indexed* Bitmap and get an *indexed* result?

Related 相关文章

  1. 1

    How do I get the length of the first line in a multi line string?

  2. 2

    How do I find out the numeric ID of the current Postgres Transaction?

  3. 3

    How do I get a value from an associative array using Swift

  4. 4

    How to get type from String?

  5. 5

    How do I get all the values of a String array to use in a return statement in a method?

  6. 6

    In JIRA Agile, when I click a quickfilter I want other quickfilters to be unclicked, how can I do this?

  7. 7

    libevent evhttp how do I get the body length or indeed any body data?

  8. 8

    Fast and safe conversion from string to numeric types

  9. 9

    How do I get a Cookie from a SoapUI response using a Groovy test step?

  10. 10

    How do I mock a method call inside the method I want to test using NUnit?

  11. 11

    In my haml file, I want to include a link within a string that is coming from a yml file

  12. 12

    Distinguish between 'character' and 'numeric' data types in Python

  13. 13

    How can I send data from a TCP Python server to a client and vice-versa?

  14. 14

    How do I catch all of these exceptions in python?

  15. 15

    I want to extract strings from a line

  16. 16

    how do retrieve data from core data table ios?

  17. 17

    How do I copy data from each record of one table into multiple records of another table related to the first table?

  18. 18

    How do I assign contents of a string to a text field in Unity 4.6?

  19. 19

    How do I get the nth derivative in my scheme program?

  20. 20

    Swift: How do I get access to the navigationController in AppDelegate

  21. 21

    How do I get the Area (or the path) of where a View is being scaffolded?

  22. 22

    How do I get my aliases to have correct completion?

  23. 23

    How do i get MS EXCEL to Connect to MYSQL Database

  24. 24

    How do I get MVC to find my controller path?

  25. 25

    How do I get Avg and Sum in Spark RDD

  26. 26

    How do I get my nested if statement to work in jQuery

  27. 27

    How do I get Ruby to search for a pattern on the tail of a local file?

  28. 28

    How do I get the computer's current language in Go?

  29. 29

    How do I efficiently crop an *indexed* Bitmap and get an *indexed* result?

热门标签

归档