Neil Cerutti <ne...@norwich.edu> writes: > On 2017-12-04, Cecil Westerhof <ce...@decebal.nl> wrote: >> Joel Goldstick <joel.goldst...@gmail.com> writes: >> >>> On Mon, Dec 4, 2017 at 5:21 AM, Ned Batchelder <n...@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 -- https://mail.python.org/mailman/listinfo/python-list