0

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
Park
  • 2,446
  • 1
  • 16
  • 25

2 Answers2

0

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.replace(f'{unit}', '')) 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.

Prox
  • 639
  • 10
  • 15
  • Since we do not know what the text format is, what is the text also contains units? What if they are comma separated? Try using `txt = "h3110 23 cat 444.4, 32.4C rabbit 11 2 dog"` – Anshumaan Mishra Apr 25 '22 at 07:17
  • @AnshumaanMishra, If they are comma separated the regex in the question would also fail. If they contain units, then ` [float(s) for s in txt.split() if '.' in s and s.replace('.','').replace(f'{unit}', '').isdigit()]` will hold. Let me edit the answer. – Prox Apr 25 '22 at 07:38
0

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']
Tomalak
  • 332,285
  • 67
  • 532
  • 628
Anshumaan Mishra
  • 1,349
  • 1
  • 4
  • 19