Python Forum
SOLVED: TTP match when final column may or may not be present - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: SOLVED: TTP match when final column may or may not be present (/thread-42417.html)



SOLVED: TTP match when final column may or may not be present - Calab - Jul-03-2024

I have another TTP problem where I can't figure out a solution...

My data:
10/0.2/0       33  49      49      48         130        0        44       5           
10/0.3/0       33  39      38      38         136        1        38       0           
10/0.0w        33  25      25      25         131        0        24       1           SomeText
As you can see, there is a final column that could be blank, or could contain a description.

I cannot figure out how to extract the 10 columns if the last column in empty.

I've tried setting a default. I've tried matching on both patterns. I've tried using an ORPHRASE for the ninth column to be split later.

This is the template I have at the moment, which only matches the last line of my data:
<group name="Interface_{{ interface }}">
{{ interface }} {{ cable_mac }} {{ total }} {{ active }} {{ registered }} {{ secondary }} {{ offline }} {{ bonding }} {{ non_bonding }} {{ chan_desc }}
</group>
How can I extract the data, even when the last column is empty?


RE: TTP match when final column may or may not be present - Calab - Jul-03-2024

Well, as I expected, after posting this question I found my answer. I don't know if it's the best solution, but it works...

Using {{ chan_desc | _line_ | strip() }} for the last column gets me what I was looking for.

<group name="interfaces">
{{ interface }} {{ cable_mac }} {{ total }} {{ active }} {{ registered }} {{ secondary }} {{ offline }} {{ bonding }} {{ non_bonding }} {{ chan_desc | _line_ | strip() }}
</group>
Including _line_ allowed the line to match regardless of the content of the 10th column. I added strip() as the description was being returned as a single space instead of an empty string.