list or dictionary

Ganesh Pal ganesh1pal at gmail.com
Wed Sep 21 15:03:14 EDT 2016


Thanks  , and it has to be re.match()

On Thu, Sep 22, 2016 at 12:18 AM, MRAB <python at mrabarnett.plus.com> wrote:

> On 2016-09-21 19:35, Ganesh Pal wrote:
>
>> Thanks Steve  for the clues , quickly tried  out # Version 1 doesn't seen
>> to work.
>>
>>
>> for line in hostname:
>>>>>
>>>> ...     regex = r'(.*) is array with id {}'.format(devid)
>> ...     mo = re.search(regex, line)
>> ...     print line, regex, mo
>> ...     if mo is not None:
>> ...         print mo.group(1)
>> ...
>> RX-145-1 is array id 1 (.*) is array with id 3 None
>> RX-145-2 is array id 2 (.*) is array with id 3 None
>> RX-145-3 is array id 3 (.*) is array with id 3 None
>>
>>> hostname
>>>>>
>>>> ['RX-145-1 is array id 1', 'RX-145-2 is array id 2', 'RX-145-3 is array
>> id
>> 3']
>>
>>> type(devid)
>>>>>
>>>> <type 'int'>
>>
>>> devid
>>>>>
>>>> 3
>> ------------------------------------------------------------
>> ---------------------------------------
>>
>>> devid = '3'
>>>>> for line in hostname:
>>>>>
>>>> ...     regex = r'(.*) is array with id {}'.format(devid)
>> ...     mo = re.search(regex, line)
>> ...     print line, regex, mo
>> ...     if mo is not None:
>> ...        print mo.group(1)
>> ...
>> RX-145-1 is array id 1 (.*) is array with id 3 None
>> RX-145-2 is array id 2 (.*) is array with id 3 None
>> RX-145-3 is array id 3 (.*) is array with id 3 None
>>
>>> type(devid)
>>>>>
>>>> <type 'str'>
>> ------------------------------------------------------------
>> ----------------------------------
>>
>>> for line in hostname:
>>>>>
>>>> ...     regex = r'(.*) is array with id %d' % (devid)
>> ...     mo = re.search(regex, line)
>> ...     print line, regex, mo
>> ...     if mo is not None:
>> ...        print mo.group(1)
>> ...
>> RX-145-1 is array id 1 (.*) is array with id 3 None
>> RX-145-2 is array id 2 (.*) is array with id 3 None
>> RX-145-3 is array id 3 (.*) is array with id 3 None
>> ------------------------------------------------------------
>> ---------------------------------------
>>
>> Looks like Iam missing something ?
>>
>> [snip]
> The lines have "array id", but the regex has "array with id".
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list