Python Forum

Full Version: Translate this line of code please
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Can someone please translate this line of code please?

while lines[0].strip()[-3:] != 'YES':

lines is a list of data(strings) returned from the w1_slave file created from a DS18B20 temperature sensor. Below is an example of what is contained within the w1_slave file

8c 01 4b 46 7f ff 04 10 2e : crc=2e YES
8c 01 4b 46 7f ff 04 10 2e t=24750

Here is the method used to create the list of lines from the w1_slave file

# read_temp_raw method to open the w1_slave file, read the lines of data and put them into a list
def read_temp_raw():
f = open(device_file, 'r') #opens the device file, w1_slave, created by the DS18B20
lines = f.readlines() #returns a list of the lines within the w1_slave file
f.close()
return lines

Going back to the original line in question:

while lines[0].strip()[-3:] != 'YES':

I think it may be stripping everything but the last three characters in the line. Is that correct?
You are correct. It's always easy to test using interactive python:
Python 3.8.1 (default, Dec 19 2019, 16:08:07) 
[GCC 7.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> lines = ['very happy YES']
>>> lines[0].strip()[-3:]
'YES'
>>>
also on future posts, please use code tags around code, errors and output
this explains how: BBCODE
Thanks for the help. I will use code tags moving forward.