How to use a regexp here

Cecil Westerhof Cecil at decebal.nl
Mon Dec 4 03:09:24 EST 2017


Neil Cerutti <neilc at norwich.edu> writes:

> On 2017-12-04, Cecil Westerhof <Cecil at decebal.nl> wrote:
>> Joel Goldstick <joel.goldstick at gmail.com> writes:
>>
>>> On Mon, Dec 4, 2017 at 5:21 AM, Ned Batchelder <ned at nedbatchelder.com>
>>> wrote:
>>>
>>>> On 12/4/17 4:36 AM, Cecil Westerhof wrote:
>>>>
>>>>> I have a script that was running perfectly for some time. It uses:
>>>>>      array = [elem for elem in output if 'CPU_TEMP' in elem]
>>>>>
>>>>> But because output has changed, I have to check for CPU_TEMP at the
>>>>> beginning of the line. What would be the best way to implement this?
>>>>>
>>>>>
>>>> No need for a regex just yet:
>>>>
>>>>     array = [elem for elem in output if elem.startswith('CPU_TEMP')]
>>>>
>>>> (btw, note that the result of this expression is a list, not an array, for
>>>> future Googling.)
>>>>
>>>> --Ned.
>>>> --
>>>> https://mail.python.org/mailman/listinfo/python-list
>>>>
>>>
>>> I like Ned's clear answer, but I'm wondering why the original code would
>>> fail because the substring is at the start of the line, since 'in' would
>>> still be true no matter where the desired string is placed.  It would be
>>> useful to see some sample data of the old data, and the new data
>>
>> There is now also a line that starts with:
>>     PCH_CPU_TEMP:
>>
>> And I do not want that one.
>
> You'll probably want to include the ':' in the startswith check,
> in case someday they also add CPU_TEMP_SOMETHING:.

I already did. And to be really sure also included a space after it.

--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof




More information about the Python-list mailing list