Home > Blockchain >  Handling Data & Sorting Results in Python
Handling Data & Sorting Results in Python

Time:12-09

I am having trouble manipulating the data returned from a Python/Kubectl query.

My goal is to get all NAMEs that are great than 30 days old (>30d).

After that, I want to load the NAMEs of these results into an array.

Python script:

import subprocess

subprocess.run(["kubectl", "get", "namespaces"])

Data returned in the terminal from Python script:

NAME                      STATUS   AGE
dread-gorge               Active   24d
dread-lagoon              Active   210d
carncier-basin            Active   164d
chantague-shallows        Active   164d
hilraine-loch             Active   311d
stangrave-waters          Active   271d

CodePudding user response:

How about this: ^([a-z] (?:-[a-z] ) ) \w (?:\d{3,}|[3-9]\d)d

https://regex101.com/r/SOmSm6/1

CodePudding user response:

Let inp is your input data (as array).

Prepare regex:
([-a-z] ) - NAME
([A-Za-z] ) - STATUS
(\d ) - AGE (last 'd' will be discarded)

\s - sequence of spaces and tabs of any length.

The first value of match will be the whole string, so indexing starts with 1 (1 - NAME, 3 - AGE)

result = []
max_days = 30
pattern = re.compile(r'([-a-z] )\s ([A-Za-z] )\s (\d )d')
for line in inp[1:]:
    match = pattern.match(line)
    if int(match[3]) > max_days:
        result.append(match[1])
  • Related