1

I have the following text

Stat indicator : 01245 Values loaded

Some irrelevant data

Stat indicator : 13452 Values loaded

My output should be giving me numbers 01245, 13452

Heres what I have tried

with open('test.txt') as fd:
    data = fd.read()
val_to_pattern = {'stat': r'Stat indicator : .{5}\n'}
val_dict = {}
for key, patt in val_to_pattern.items():
    val_dict[key] = re.findall(patt, data)
0

3 Answers 3

1

Use

val_to_pattern = {'stat': r'Stat indicator\s*:\s*(\d+)'}

Note that \s*:\s* pattern matches a : optionally wrapped with 0+ whitespaces and the (\d+) part matches and captures into Group 1 any 0+ digits (note that re.findall only returns captured substrings if capturing groups are set in the regex).

See the regex demo (the green text will be the result).

Alternatively, if the number of digits in the indicator stats is always 5 use either of

 val_to_pattern = {'stat': r'Stat indicator\s*:\s*(\d{5})\b'}
 val_to_pattern = {'stat': r'Stat indicator\s*:\s*(\d{5})(?!\d)'}

\b is word boundary that will require a non-word char or end of string after 5 digits and the (?!\d) is a negative lookahead that fails the match if there is a digit immediately to the right of the current location.

Sign up to request clarification or add additional context in comments.

Comments

0

You can used regex \d, and if you know the size of your number you can use {size} with it.

\d{3} Returns a match where the string contains 3 digits (numbers from 0-9)

\d{3,5} Returns a match where the string contains 3 to 5 digits (numbers from 0-9)

So you can used r'\d{3,5}' as regex pattern

import re

with open('test.txt') as fd:
    data = fd.read()
val_to_pattern = {'stat': r'\d{5}'}
val_dict = {}
for key, patt in val_to_pattern.items():
    re_find = re.findall(patt, data)
    val_dict[key] = re.findall(patt, data)

Comments

0

If you don't want to use regex, you could implement a straightforward loop.

  1 with open('test.txt') as fd:
  2     data = fd.read()
  3
  4 nums = []
  5 temp = ""
  6 is_dig = False
  7 for char in data:
  8      if char.isdigit():
  9           temp += char
 10           is_dig = True
 11      elif is_dig:
 12           nums.append(temp)
 13           temp = ""
 14           is_dig = False
 15 print(nums)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.