Translate this line of code please - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Translate this line of code please (/thread-24129.html) |
Translate this line of code please - keystone76 - Jan-31-2020 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? RE: Translate this line of code please - Larz60+ - Jan-31-2020 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 RE: Translate this line of code please - keystone76 - Jan-31-2020 Thanks for the help. I will use code tags moving forward. |