I am using an arduino uno and a thermistor to measure the current temperature. I have been using re.findall to find the matching string in line 4, is there an alternative instead of using re.findall that has the same function? as I am not allowed to use re in my project. Thanks
def my_function(port):
# Set COM Port.....
ser = serial.Serial(port, 9600, timeout=0,
parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, rtscts=0)
my_function('COM3')
# Set path to my Arduino device
#portPath = my_function
baud = 9600
sample_time = 1 #Takes temperature every 1 second
sim_time = 1 #Graphs 5 data points
# Initializing Lists
# Data Collection
data_log = []
line_data = []
# Establishing Serial Connection
connection = serial.Serial("com3",baud)
# Calculating the length of data to collect based on the
# sample time and simulation time (set by user)
max_length = sim_time/sample_time
# Collecting the data from the serial port
while True:
line = connection.readline()
line_data = re.findall('\d*\.\d*',str(line))
line_data = filter(None,line_data)
line_data = [float(x) for x in line_data]
line_data = [(x-32)*0.5556 for x in line_data] #list comprehension to convert line_data temp to celsius
line_data = [ round(elem,2) for elem in line_data] #round temp to 2 dp
if len(line_data) > 0:
print("The current temperature is:" str(line_data[0]) " celsius")
break
CodePudding user response:
Since no sample output is given, Here is a code that extracts all valid numbers from the text:
a = "Temperature = 54.3F 62.5, 79999 54.3°C 23.3C"
a =' '
temp = []
i = 0
while i < len(a):
if (a[i].isdigit() or a[i] == '.'):
if a[i] == '.' and '.' in temp[-1]:
x = a.find(' ', i)
i = x
temp.pop()
elif i == 0:
temp[-1] = a[i]
elif len(temp)>0 and a[i-1] == temp[-1][-1]:
temp[-1] = a[i]
else:
temp.append(a[i])
i = 1
temp = list(map(float, temp)) # Float casting
print(temp)
Output:
['54.3', '62.5', '79999', '54.3', '23.3']
CodePudding user response:
Looking at this answer here: https://stackoverflow.com/a/4289557/7802476 A slight modification can also give decimals:
>>> txt = "h3110 23 cat 444.4 rabbit 11 2 dog"
>>> [float(s) for s in txt.split() if '.' in s and s.replace('.','').isdigit()]
>>> [444.4]
Your regex \d*\.\d*
will match numbers such as {.2, 2., 2.2,...}
but will not match {2}
since \.
has to be in the number. The above will also do the same.
EDIT:
The solution won't handle numbers that are attached to a string {2.2°C}
where as the regex does.
To make it handle units as well,
[float(s) for s in txt.split() if '.' in s and s.replace('.','').replace(f'{unit}', '').isdigit()]
Where unit
can be '°C'
or 'F'
for temperature.
However, your regex matches all floating point numbers attached to any string. That is, cat2.2rabbit
would also return 2.2
, not sure if this should be returned.