简体   繁体   English

python 的 re.findall 替代方案

[英]Alternative for re.findall for python

I am using an arduino uno and a thermistor to measure the current temperature.我正在使用 arduino uno 和热敏电阻来测量当前温度。 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?我一直在使用 re.findall 来查找第 4 行中的匹配字符串,是否有替代方法而不是使用具有相同 function 的 re.findall? as I am not allowed to use re in my project.因为我不允许在我的项目中使用 re。 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

Looking at this answer here: https://stackoverflow.com/a/4289557/7802476 A slight modification can also give decimals:在这里查看这个答案: https://stackoverflow.com/a/4289557/7802476稍微修改一下也可以给出小数:

>>> 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 \.您的正则表达式\d*\.\d*将匹配{.2, 2., 2.2,...}等数字,但不会匹配{2} ,因为\. 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.编辑:该解决方案不会像正则表达式那样处理附加到字符串{2.2°C}的数字。

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.其中温度unit可以是'°C''F'

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.也就是说, cat2.2rabbit也会返回2.2 ,不确定是否应该返回。

Since no sample output is given, Here is a code that extracts all valid numbers from the text:由于没有给出样本 output,下面是从文本中提取所有有效数字的代码:

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: Output:

['54.3', '62.5', '79999', '54.3', '23.3']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM