-2

I'm trying to take 500+ lines of data/values, and each line looks like

random random data: 7 random height: 7
random words data: 20
random height: 8 random random data: 522 random height: 2
random random data: 1 random height: 10

And I'm just trying to extract the height values, and print them to the screen

I would think I would use some sort of split command but not sure where to start (I'm very new to python), if the above 4 lines is my data set then how can I approach this?

mystring = """
random random data: 7 random height: 7
random words data: 20
random height: 8 random random data: 522 random height: 2
random random data: 1 random height: 10
"""

something like that?

  • if you want to extract the height numbers and you know the format is always going to be `height: DIGITS random` then you can use a simple regex `r'height:\s+(\d+)\s'` – Nullman Mar 10 '22 at 10:58
  • Does this answer your question? [How to extract the substring between two markers?](https://stackoverflow.com/q/4666973/6045800) – Tomerikoo Mar 10 '22 at 11:01
  • @Nullman ah yes that could possibly work. I try to add that to the mystring part I wrote in the question but it doesn't really do anything. Would I write the r'height part then print something? – tokenidentity22 Mar 10 '22 at 11:05
  • Really? Did you pass through all 20 answers and none of them even gave you a direction? – Tomerikoo Mar 10 '22 at 11:07
  • look at Arthur king's answer below, it gives you a full example – Nullman Mar 10 '22 at 11:09
  • @Tomerikoo yeah unfortunately. tried a few but didn't quite work. thanks – tokenidentity22 Mar 10 '22 at 11:13
  • Does this answer your question? [How to grab number after word in python](https://stackoverflow.com/q/7548692/6045800) – Tomerikoo Mar 10 '22 at 11:17
  • @Nullman yep yourself and Arthur King have helped perfectly. I'm now looking into regex, thanks for mentioning that. Sometimes there is a minus on the value, any idea how to allow that to be captured too? (Height was an example) – tokenidentity22 Mar 10 '22 at 11:29
  • easpy peasy, change arthur's regex like this: `'height:\s(-?\d+)'` (question mark means 0 or 1 occurences) – Nullman Mar 10 '22 at 11:35
  • @Nullman, nice one, thanks for the help – tokenidentity22 Mar 10 '22 at 11:41

1 Answers1

1

A simple regular expression should help:

import re
mystring = """
random random data: 7 random height: 7
random words data: 20
random height: -8 random random data: 522 random height: 2
random random data: 1 random height: 10
"""
for height_value in re.findall('height:\s(-?\d+)', mystring):
    print(height_value)

Output:

7
-8
2
10
DarkKnight
  • 19,739
  • 3
  • 6
  • 22
  • Awesome! That 'height:\s(\d+)' command worked great. I just do have one additional question. Sometimes there is a minus on the value, any idea how to allow that to be captured too? (Height was an example) – tokenidentity22 Mar 10 '22 at 11:28
  • @lrod41 I've edited the expression and data to show how you could handle a minus sign – DarkKnight Mar 10 '22 at 11:38