Python .strip() method

Coming to the part where i need to get the value of counters and sensors and display them on the screen, I need to extract part of the raw reply data. It is totally different from the rest which i simply use if and else to check the conditions.

The reply that I will get from readlines for Counter and Sensor is in the format of:
[‘/x03C?nnss/r’] and [‘/x03s?nnssss/r’] respectively, where nn is the counter or sensor number and ss is the status value in Hex. Sensor can support up to 2 bytes. The most significant byte is on the right. e.g FF00 – FF is least significant byte and 00 is most significant byte.
The data that i would like to display is only the “ss” part. So, to extract “ss” from the readlines reply, the method str.strip(chars) is used. It “returns a copy of the string with the leading and trailing characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted, the chars argument defaults to removing whitespace.”

As i would like to extract last 2 characters from the reply of counter, i execute:
status="Count= %sH"%response[0].strip()[-2:]

Then for sensor, i execute:
status="Sensor= %sH"%response[0].strip()[-4:]

Here’s a good source of reference: Python Doc and forum about slice.

One thing i don’t like about stackoverflow is that some people are too arrogant and they just love to step on beginners’ heads. :T Hmmp!

Points to note for myself:
backslash “\” is required to display special character like ‘doesn\’t’ will give “doesn’t”
slice method [:] is useful to extract the characters in the string. So, [-2:] will give the last 2 characters.

Good to remember:

 +---+---+---+---+---+
 | H | e | l | p | A |
 +---+---+---+---+---+
 0   1   2   3   4   5
-5  -4  -3  -2  -1

 

KIV tmr: https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Regular_Expressions

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s