1

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

1
  • what do you mean reliable? Commented May 13, 2019 at 12:05

1 Answer 1

5

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'}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.