python 将键值对转为字典
创建key_values_to_dic.py
def key_values_to_dic(key_values_):
"""
将键值对转换为字典形式
:param key_values_:
:return:
"""
key_values_dic_ = {}
for line in key_values_.split("\n"):
line = line.strip()
if not line:
continue
try:
key, value = line.split(":", 1)
key = key.strip()
key_values_dic_[key] = value
except ValueError:
print("ERROR: 键值对错误,转换失败", line)
return key_values_dic_
key_values = """
appid:wx782c26e4c19acffb
redirect_uri:https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxnewloginpage
fun:new
lang:zh_CN
_:1546083528637
"""
print(key_values_to_dic(key_values))