Search code examples
pythonvariablestextstoring-information

How to mine Specified from text in python?


I have an output from a program as a text like:

------------
Action specified: GetInfo

Gathering information...

Reported chip type: 2307

Reported chip ID: 98-DE-94-93-76-50

Reported firmware version: 1.08.10

------------

But I must save just, Reported chip type: value "2307" in a variable. how it's possible?


Solution

  • You would usually do something like this with regex

    import re
    match = re.search('Reported chip type:\s(?P<chip_type>\d+)', my_text)
    chiptype = int(match.group('chip_type'))     
    
    >>> print chiptype
    2307
    

    In your case though, it's probably simple enough to just use a couple splits:

    chiptype = int(my_text.split('Reported chip type:', 1)[-1].split('\n')[0].strip())