How to use a regexp here

Joel Goldstick joel.goldstick at gmail.com
Mon Dec 4 05:41:21 EST 2017


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

-- 
Joel Goldstick
http://joelgoldstick.com/blog
http://cc-baseballstats.info/stats/birthdays



More information about the Python-list mailing list