Collections之OrderedDict()

def str_to_dic(headerStr, stripIsNotvalid=None):
    dict = collections.OrderedDict() #对字典对象中元素的排序。
    if headerStr.strip():
        for headItem in headerStr.split("\n"):
            parm = headItem.split(":")
            if stripIsNotvalid:
                dict[parm[0]] = headItem[(headItem.index(parm[0]) + parm[0].__len__() + 1):].lstrip()
            else:
                dict[parm[0]] = headItem[(headItem.index(parm[0]) + parm[0].__len__() + 1):].strip()
    print(dict)
    return dict

pageHeader = str_to_dic(
    "Host: ahbbzy.ts.sifayun.com\n" +
    "Connection: keep-alive\n" +
    "Upgrade-Insecure-Requests: 1\n" +
    "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36\n" +
    "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9\n" +
    "Accept-Encoding: gzip, deflate\n" +
    "Accept-Language: zh-CN,zh;q=0.9")

 

posted @ 2021-05-07 11:10  Eliphaz  阅读(83)  评论(0编辑  收藏  举报