用于转换的字符串

时间:2017-03-17 06:12:11

标签: python django

我想在字典中转换字符串,我的字符串如下

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=

7 个答案:

答案 0 :(得分:1)

您可以使用split方法拆分此字符串:

c="order_id=BW_225996&tracking_id=306003083135"

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

输出:

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

答案 1 :(得分: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('&')]}

答案 2 :(得分:0)

您可以尝试:

>>> 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'}

答案 3 :(得分:0)

一个简单的循环可以解决这个问题:

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('&')]

答案 4 :(得分:0)

请尝试这个简单的代码,这将产生您所需的输出,

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

输出:

{'order_id': 'BW_225996', 'tracking_id': '306003083135', 'bank_ref_no': '1489730168508'}
  • 我们最初使用'&'分割字符串。
  • 然后我们使用'='作为字典键来分割列表中的每个项目 值。

请让我知道任何疑问的内容。

答案 5 :(得分:0)

这是一个能为你完成工作的功能。它执行以下操作:

  1. 将给定字符串分解为表示键值对的字符串列表。
  2. 将键值对的每个字符串表示形式拆分为键及其关联值。
  3. 将每个密钥及其相关值插入dict
  4. <强> 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
    

    使用示例:

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

    输出:

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

    我希望这能回答你的问题。快乐的编码!

答案 6 :(得分:0)

由于此问题已标记为,因此这是最短且最正确的解决方案:

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

与其他解决方案不同,它会自动处理每个键的url编码和多个值(使用getlist())。