Bhavesh Ghodasara
Bhavesh Ghodasara

Reputation: 2071

Matching multiple strings

I am learning Python string operations and trying to convert delimited text into variables.

i.e. "On Tap: 20 | Bottles: 957 | Cans: 139"

This string should assign value of 20 to Tap, 957 to Bottles, and 139 to Cans. This string is not fixed and may vary (for example 3 values or 0, also position of Tap, Bottles or Cans can be interchanged).

So far I have developed this:

import re

strEx = "On Tap: 20 | Bottles: 957 | Cans: 139"
barServingText = strEx.split('|')
print(barServingText)
for i in barServingText:
    print (i)
    if i.find("Bottles"):
        print("Found Bottles")
        Bottles = re.sub('[^0-9]*','',i)
        print(Bottles)
    elif i.find("Cans"):
        print("Found Cans")
        Cans = re.sub('[^0-9]*','',i)
        print(Cans)
    elif i.find("Tap"):
        print("Found Tap")
        Tap = re.sub('[^0-9]*','',i)
        print(Tap)

However it is not working as per my expectations and reassigning the value of Bottles every time.

Output:

['On Tap: 20 ', ' Bottles: 957 ', ' Cans: 139']
On Tap: 20
Found Bottles
20
 Bottles: 957
Found Bottles
957
 Cans: 139
Found Bottles
139

I have included many print statements to debug the code. My purpose is just to assign values to proper variables.

Upvotes: 1

Views: 1668

Answers (3)

Mustofa Rizwan
Mustofa Rizwan

Reputation: 10466

The following regex should create key value pair for you:

r"((.*?):(.*?)(\||$))"

The following approach however i think is better suited as it would make it dynamic and can have more than these 3 variables

import re

regex = ur"((.*?):(.*?)(\||$))"

test_str = u"On Tap: 20 | Bottles: 957 | Cans: 139"

matches = re.finditer(regex, test_str)

for matchNum, match in enumerate(matches):
    s=match.group(2).strip().split(' ')[-1]+"="+match.group(3).strip()
    print(s)
    exec(s)

print(Tap)
print(Bottles)
print(Cans)

Upvotes: 1

Ben Morris
Ben Morris

Reputation: 626

The str.find() method is used for returning the location of the text in a string. If it doesn't find the text, it returns the integer -1. In Python, for checking if on string contains another, you may want to use the syntax if subString in string:, like so:

...
    if "Bottles" in i:
        print("Found Bottles")
...

As the official documentation states:

For the string and byte types, x in y is only true if and only if x is a substring of y. An equivalent test is y.find(x) != -1

So, depending on your preferred coding style and/or particular needs, you can choose between "x in y" or "y.find(x) != -1"

Upvotes: 1

furas
furas

Reputation: 142631

find returns -1 when it can't find string and -1 is treated as True (bool(-1) gives True) so you have to use find(...) != -1

import re

strEx = "On Tap: 20 | Bottles: 957 | Cans: 139"
barServingText = strEx.split('|')
print(barServingText)
for i in barServingText:
    print (i)
    if i.find("Bottles") != -1:
        print("Found Bottles")
        Bottles = re.sub('[^0-9]*','',i)
        print(Bottles)
    elif i.find("Cans") != -1:
        print("Found Cans")
        Cans = re.sub('[^0-9]*','',i)
        print(Cans)
    elif i.find("Tap") != -1:
        print("Found Tap")
        Tap = re.sub('[^0-9]*','',i)
        print(Tap)

BTW: with your data you don't need re. You can use split (and strip)

Bottles = i.split(':')[1].strip()

Cans = i.split(':')[1].strip()

Tap = i.split(':')[1].strip()

Upvotes: 3

Related Questions