[Fixed]-String to dict conversion

1👍

You can use split method to split this string:

c="order_id=BW_225996&tracking_id=306003083135"

print {p.split("=")[0]:p.split("=")[1] for p in c.split("&")}

Output:

{'order_id': 'BW_225996', 'tracking_id': '306003083135'}

0👍

_str="order_id=BW_225996&tracking_id=306003083135&bank_ref_no=1489730168508&order_status=Success&failure_message=&payment_mode=Net Banking&card_name=AvenuesTest&status_code=null&status_message=Y&currency=INR&amount=100.0&billing_name=test&billing_address=test&billing_city=Pune&billing_state=Maharashtra&billing_zip=411041&billing_country=India&billing_tel=1234567890&billing_email=test@gmail.com&delivery_name=test&delivery_address=test&delivery_city=Pune&delivery_state=Maharashtra&delivery_zip=411041&delivery_country=India&delivery_tel=1234567890&merchant_param1=3&merchant_param2=&merchant_param3=&merchant_param4=&merchant_param5=&vault=N&offer_type=null&offer_code=null&discount_value=0.0&mer_amount=100.0&eci_value=null&retry=N&response_code=0&billing_notes=&trans_date=17/03/2017 11:27:30&bin_country="

{a:b for a,b in [i.split("=") for i in _str.split('&')]}

0👍

You can try:

>>> s = """order_id=BW_225996&tracking_id=306003083135&bank_ref_no=1489730168508&order_status=Success&failure_message=&payment_mode=Net Banking&card_name=AvenuesTest&status_code=null&status_message=Y&currency=INR&amount=100.0&billing_name=test&billing_address=test&billing_city=Pune&billing_state=Maharashtra&billing_zip=411041&billing_country=India&billing_tel=1234567890&billing_email=test@gmail.com&delivery_name=test&delivery_address=test&delivery_city=Pune&delivery_state=Maharashtra&delivery_zip=411041&delivery_country=India&delivery_tel=1234567890&merchant_param1=3&merchant_param2=&merchant_param3=&merchant_param4=&merchant_param5=&vault=N&offer_type=null&offer_code=null&discount_value=0.0&mer_amount=100.0&eci_value=null&retry=N&response_code=0&billing_notes=&trans_date=17/03/2017 11:27:30"""
>>> s_list = s.split("&")
>>> s_list
['order_id=BW_225996', 'tracking_id=306003083135', 'bank_ref_no=1489730168508', 'order_status=Success', 'failure_message=', 'payment_mode=Net Banking', 'card_name=AvenuesTest', 'status_code=null', 'status_message=Y', 'currency=INR', 'amount=100.0', 'billing_name=test', 'billing_address=test', 'billing_city=Pune', 'billing_state=Maharashtra', 'billing_zip=411041', 'billing_country=India', 'billing_tel=1234567890', 'billing_email=test@gmail.com', 'delivery_name=test', 'delivery_address=test', 'delivery_city=Pune', 'delivery_state=Maharashtra', 'delivery_zip=411041', 'delivery_country=India', 'delivery_tel=1234567890', 'merchant_param1=3', 'merchant_param2=', 'merchant_param3=', 'merchant_param4=', 'merchant_param5=', 'vault=N', 'offer_type=null', 'offer_code=null', 'discount_value=0.0', 'mer_amount=100.0', 'eci_value=null', 'retry=N', 'response_code=0', 'billing_notes=', 'trans_date=17/03/2017 11:27:30']
>>> s_dict = {}
>>> for data in s_list:
    s_dict[data.split("=")[0]] = data.split("=")[1]


>>> print s_dict
{'billing_tel': '1234567890', 'status_code': 'null', 'delivery_country': 'India', 'delivery_name': 'test', 'currency': 'INR', 'delivery_city': 'Pune', 'billing_country': 'India', 'billing_notes': '', 'retry': 'N', 'billing_email': 'test@gmail.com', 'billing_zip': '411041', 'billing_name': 'test', 'merchant_param5': '', 'order_status': 'Success', 'status_message': 'Y', 'mer_amount': '100.0', 'merchant_param3': '', 'merchant_param2': '', 'delivery_zip': '411041', 'card_name': 'AvenuesTest', 'delivery_tel': '1234567890', 'billing_address': 'test', 'order_id': 'BW_225996', 'eci_value': 'null', 'offer_code': 'null', 'merchant_param4': '', 'payment_mode': 'Net Banking', 'offer_type': 'null', 'discount_value': '0.0', 'delivery_address': 'test', 'billing_city': 'Pune', 'merchant_param1': '3', 'response_code': '0', 'failure_message': '', 'bank_ref_no': '1489730168508', 'amount': '100.0', 'trans_date': '17/03/2017 11:27:30', 'tracking_id': '306003083135', 'vault': 'N', 'delivery_state': 'Maharashtra', 'billing_state': 'Maharashtra'}

0👍

A simple loop could fix this:

a='order_id=BW_225996&tracking_id=306003083135&bank_ref_no=1489730168508&order_status=Success&failure_message=&payment_mode=Net Banking&card_name=AvenuesTest&status_code=null&status_message=Y&currency=INR&amount=100.0&billing_name=test&billing_address=test&billing_city=Pune&billing_state=Maharashtra&billing_zip=411041&billing_country=India&billing_tel=1234567890&billing_email=test@gmail.com&delivery_name=test&delivery_address=test&delivery_city=Pune&delivery_state=Maharashtra&delivery_zip=411041&delivery_country=India&delivery_tel=1234567890&merchant_param1=3&merchant_param2=&merchant_param3=&merchant_param4=&merchant_param5=&vault=N&offer_type=null&offer_code=null&discount_value=0.0&mer_amount=100.0&eci_value=null&retry=N&response_code=0&billing_notes=&trans_date=17/03/2017 11:27:30&bin_country='

dic={}
[dic.update({e.split('=')[0] : e.split('=')[1]}) for e in a.split('&')]

0👍

Please try this simple code, this will produce your required output,

input_string = 'order_id=BW_225996&tracking_id=306003083135&bank_ref_no=1489730168508'

splitted_string = input_string.split('&')

final_output = {}

for items in splitted_string:
    dic_values = items.split('=')
    final_output[dic_values[0]] = dic_values[1]

print final_output

Output:

{'order_id': 'BW_225996', 'tracking_id': '306003083135', 'bank_ref_no': '1489730168508'}
  • Initially we are splitting the string using ‘&’.
  • Then we are splitting every items in list using ‘=’ as the dictionary key and
    values.

Please let me know interms of any queries.

0👍

Here is a function that will do the job for you. It performs the following operations:

  1. Break the given string into a list of strings representing key-value pairs.
  2. Split each string representation of a key-value pair into a key and its associated value.
  3. Insert each key and it’s associated value into a dict.

parse_dict

def parse_dict(string):
    dictionary = dict()
    key_value_pairs = string.split('&')
    for pair in key_value_pairs:
        key, value = pair.split('=')
        dictionary[key] = value
    return dictionary

Example usage:

parsed_dict = parse_dict("order_id=BW_225996&tracking_id=306003083135")
print(parsed_dict) 

Output:

{'order_id': 'BW_225996', 'tracking_id': '306003083135'}

I hope this answers your question. Happy coding!

0👍

Since this question is tagged , this is the shortest and most correct solution:

from django.http import QueryDict

query_string = 'order_id=BW_225996&tracking_id=306003083135&bank_ref_no=1489730168508'
qdict = QueryDict(query_string) # dict like object

Unlike the other solutions, this deals with url encoding and multiple values per key automatically (use getlist()).

Leave a comment