Palla veera
Palla veera

Reputation: 123

Python filter data from given string

I have below data:

data = """
item: apple
store name: USA_1
store id: 1000
total: 200

item: apple
store name: USA_2
store id: 1001
total: 230

item: apple
store name: USA_3
store id: 1002
total: 330

item: apple
store name: UK1
store id: 2000
total: 20

item: apple
store name: UK_2
store id: 1021
total: 230
"""

I have to get store dict format like below:

{' USA_1': ' 1000', ' USA_2': ' 1001', ' USA_3': ' 1002', ' UK1': ' 2000', ' UK_2': ' 1021'}

I have written below code which will obtain above output:

STORE_NAME_GATHERED = []
STORE_IDS_GATHERED = []
STORE_info = {}
for line in data.split("\n"):
    line = line.strip()
    if line.startswith("store name:"):
        name = line.split(":")[1]
        if not name in  STORE_NAME_GATHERED:
            STORE_NAME_GATHERED.append(name)
    elif line.startswith("store id:"):
        id = line.split(":")[1]
        if not id in STORE_IDS_GATHERED:
            STORE_IDS_GATHERED.append(id)
            STORE_info[name] = id
print(STORE_info)

I am getting expected results from above code, However it is not a best code to achieve above output and not reliable, Can some one help me with right code to achieve same results with reliable manner

Upvotes: 1

Views: 67

Answers (1)

Rakesh
Rakesh

Reputation: 82765

Using regex

Ex:

import re


data = """
item: apple
store name: USA_1
store id: 1000
total: 200

item: apple
store name: USA_2
store id: 1001
total: 230

item: apple
store name: USA_3
store id: 1002
total: 330

item: apple
store name: UK1
store id: 2000
total: 20

item: apple
store name: UK_2
store id: 1021
total: 230
"""

name = re.findall(r"store name: (.*)", data)   #Get Store Name
store = re.findall(r"store id: (.*)", data)    #Get Store ID

print(dict(zip(name, store)))

Output:

{'UK1': '2000',
 'UK_2': '1021',
 'USA_1': '1000',
 'USA_2': '1001',
 'USA_3': '1002'}

Upvotes: 5

Related Questions