Python Forum

Full Version: parsing question
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have the following :
Output:
b'011;005;080;'
What is the best way to parse this to remove all and get 005 (middle term)?
(Oct-06-2020, 12:40 AM)ridgerunnersjw Wrote: [ -> ]I have the following :
Output:
b'011;005;080;'
What is the best way to parse this to remove all and get 005 (middle term)?

You can use regular expression as the example below:

import re
v_ent = "b'011;005;080;'"
v_sub = "005"
v_out = re.sub(r"b'011;005;080;'", v_sub, v_ent)
print(v_out)
That seems to work quite well except my value that I gave is only an example and the three values are data read from an IC. They change. I need to be able to basically read then parse to get the inner 3 values...The following quasi works but it puts a newline after printing each value...I tried value.strip() but it doesn't seem to work

	def DataParser(self, parse_value):
		for value in parse_value:
			if (value == ':'):
				print ('\n')
			else:
				print (value)
Output:
>>> b'011;005;080;'.split(b';')[1] b'005'
Though a more generic regular expression may be faster. You probably don't need that, but if you do it's worth the effort to read a basic tutorial on regexes.