Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Stripping numbers from text
#1
Hi,

The response from a web form fills an array. Its content may average from empty to several strings.
I need to extract data from said strings so I can work with it.
I came out with the following code but I'm sure it can be improved.
I appreciate some opinion/improvement.

TIA

import re

soil1 = 0
soil2 = 0
checkbox1 = 0
checkbox2 = 0

para_array = ['s1=2.67', 's2=34', 'p1=1', 'p2=1']

for i in range(len(para_array)):
    if para_array[i].find('s1=') > -1:
        a = para_array[i]
        soil1 = re.sub('s1=', '', a)
    if para_array[i].find('s2=') > -1:
        a = para_array[i]
        soil2 = re.sub('s2=', '', a)
    if para_array[i].find('p1=') > -1:
        a = para_array[i]
        checkbox1 = re.sub('p1=', '', a)
    if para_array[i].find('p2=') > -1:
        a = para_array[i]
        checkbox2 = re.sub('p2=', '', a)

print(soil1, soil2, checkbox1, checkbox2)
Reply
#2
Can use regex.
>>> import re
>>> 
>>> para_array = ['s1=2.67', 's2=34', 'p1=1', 'p2=1']
>>> for item in para_array:
...     print(re.search(r'=(\d.*)', item).group(1))
...     
2.67
34
1
1
Reply
#3
(Jul-18-2022, 02:43 PM)snippsat Wrote: Can use regex.
>>> import re
>>> 
>>> para_array = ['s1=2.67', 's2=34', 'p1=1', 'p2=1']
>>> for item in para_array:
...     print(re.search(r'=(\d.*)', item).group(1))

Slick, thank you. However, it crashes when the webform returns no data like this:
Output:
para_array = ['s1=2.67', 's2=', 'p1=', 'p2=1']
Also, how do I associate the output of the loop to a variable like:
soil1 = 2.67
soil2 = 34 etc.
Reply
#4
(Jul-18-2022, 03:18 PM)ebolisa Wrote: Slick, thank you. However, it crashes when the webform returns no data like this:
That can be fixed,you should give it a try🔨
Quote:Also, how do I associate the output of the loop to a variable like:
It's a bad way🖐 to crate a bunch of variables this way,use a data structure as eg dictionary.
Example.
import re

def clean_up(lst : list) -> list:
    fix_lst = []
    try:
        for item in para_array:
            if item.endswith('='):
                pass
            else:
                fix_lst.append(re.search(r'=(\d.*)', item).group(1))
        return fix_lst
    except AttributeError:
        return 'Something went wrong'

if __name__ == '__main__':
    para_array = ['s1=2.67', 's2=34', 'p1=1', 'p2=1']
    #para_array = ['s1=2.67', 's2=', 'p1=', 'p2=1']
    result = clean_up(para_array)
    var = [f'soil{i}' for i in range(1, 5)]
    record = dict(zip(var, result))
    print(record)
Output:
{'soil1': '2.67', 'soil2': '34', 'soil3': '1', 'soil4': '1'}
Eg if use dictionary like this,it will not fail(give message) if search for value not in record.
 >>> record.get('soil2', 'No in record')
'34'
>>> record.get('soil4', 'No in record')
'1'
>>> record.get('soil9', 'No in record')
'No in record
Reply
#5
I don't think ebolisa wants to create new variables. I think the question is wondering how to make parsing "s1 = 2.67" result in assigning the value 2.67 to existing variable soil1. But your solution works for this problem too. There should not be a variable named soil1, there should be a dictionary of external parameters that can be set through the para_array. Instead of the program using soil1 it should use parameters["s1"]. An alternative (better alternative) is to use a class instead of a dictionary. The class would be responsible for parsing the para_array and it would know that "s1" is associated with the attribute soil1. If a class were used you would get the soil1 value using obj.soil1.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Print Numbers starting at 1 vertically with separator for output numbers Pleiades 3 3,748 May-09-2019, 12:19 PM
Last Post: Pleiades
  Looking for simple help - text file stripping DerekK 1 2,136 Mar-08-2019, 10:06 AM
Last Post: Larz60+
  Help with stripping output oldcity 6 3,443 Oct-28-2018, 07:40 PM
Last Post: oldcity

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020