python_test_36

50.题目:
有一个字符串my_str=”BAIDUID=E764B0EC171C49E775FF397A900736AD;FG=1;PSTM=1557215582; BIDUPSID=6923B20763EDF8F924B70500ADEB3A39;
BDORZ=B490B5EBF6F3CD402E515D22BCDA1598;BDSFRCVID=hdLOJeC62rInqlJ9FJCTrg6P-yw-3krTH6aoLNdMABLO8IBXrUrTEG0PjU8g0KubYGzWogKKBeOTHn0F_2uxOjjg8UtVJeC6EG0P3J;”
请将它转换为字典格式(=前面的是key,后面是value,中间没有空格和换行)
{“BAIDUID”:”E764B0EC171C49E775FF397A900736AD”}

my_str = "BAIDUID=E764B0EC171C49E775FF397A900736AD;FG=1;PSTM=1557215582; BIDUPSID=6923B20763EDF8F924B70500ADEB3A39;BDORZ=B490B5EBF6F3CD402E515D22BCDA1598;BDSFRCVID=hdLOJeC62rInqlJ9FJCTrg6P-yw-3krTH6aoLNdMABLO8IBXrUrTEG0PjU8g0KubYGzWogKKBeOTHn0F_2uxOjjg8UtVJeC6EG0P3J;"
a = my_str.split(";")
print(a)
c = dict()

for var in a:
    if var != "":
        b = var.split("=")
        c[b[0]] = b[1]
print(c)
c = {var.split("=")[0]: var.split("=")[1] for var in my_str.split(";") if var != ""}
print(c)

输出结果是:

['BAIDUID=E764B0EC171C49E775FF397A900736AD', 'FG=1', 'PSTM=1557215582', ' BIDUPSID=6923B20763EDF8F924B70500ADEB3A39', 'BDORZ=B490B5EBF6F3CD402E515D22BCDA1598', 'BDSFRCVID=hdLOJeC62rInqlJ9FJCTrg6P-yw-3krTH6aoLNdMABLO8IBXrUrTEG0PjU8g0KubYGzWogKKBeOTHn0F_2uxOjjg8UtVJeC6EG0P3J', '']
{'BAIDUID': 'E764B0EC171C49E775FF397A900736AD', 'FG': '1', 'PSTM': '1557215582', ' BIDUPSID': '6923B20763EDF8F924B70500ADEB3A39', 'BDORZ': 'B490B5EBF6F3CD402E515D22BCDA1598', 'BDSFRCVID': 'hdLOJeC62rInqlJ9FJCTrg6P-yw-3krTH6aoLNdMABLO8IBXrUrTEG0PjU8g0KubYGzWogKKBeOTHn0F_2uxOjjg8UtVJeC6EG0P3J'}
{'BAIDUID': 'E764B0EC171C49E775FF397A900736AD', 'FG': '1', 'PSTM': '1557215582', ' BIDUPSID': '6923B20763EDF8F924B70500ADEB3A39', 'BDORZ': 'B490B5EBF6F3CD402E515D22BCDA1598', 'BDSFRCVID': 'hdLOJeC62rInqlJ9FJCTrg6P-yw-3krTH6aoLNdMABLO8IBXrUrTEG0PjU8g0KubYGzWogKKBeOTHn0F_2uxOjjg8UtVJeC6EG0P3J'}

你可能感兴趣的:(Python_learning)