Python Forum
Translate this line of code please
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Translate this line of code please
#1
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?
Reply
#2
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
Reply
#3
Thanks for the help. I will use code tags moving forward.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unable to understand the meaning of the line of code. jahuja73 0 268 Jan-23-2024, 05:09 AM
Last Post: jahuja73
  googletrans library to translate text language for using data frame is not running gcozba2023 0 1,162 Mar-06-2023, 09:50 AM
Last Post: gcozba2023
  Trying to loop through code to plot seaborn line plots across multiple subplots eyavuz21 0 1,574 Dec-05-2022, 10:46 AM
Last Post: eyavuz21
  BeautifulSoup - I can't translate html tags that contain <a href=..</a> OR <em></em> Melcu54 10 1,563 Oct-27-2022, 08:58 AM
Last Post: wavic
  Python code to read second line from CSV files and create a master CSV file sh1704 1 2,353 Feb-13-2022, 07:13 PM
Last Post: menator01
  How to Translate a python code written in Mac-OS to Windows? alexanderDennisEnviro500 2 2,638 Jul-31-2021, 08:36 AM
Last Post: Gribouillis
  how long can a line of code be? Skaperen 2 2,172 Jun-09-2021, 06:31 PM
Last Post: Skaperen
  Delimiters - How to skip some html tags from being translate Melcu54 0 1,619 May-26-2021, 06:21 AM
Last Post: Melcu54
  I need a code line to spam a keyboard key | Image detection bot Aizou 2 3,044 Dec-06-2020, 10:10 PM
Last Post: Aizou
  Line of code to show dictionary doesn't work MaartenRo 2 2,397 Jul-28-2020, 03:58 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020