How to convert cookie string to Python dictionary
Tags: python, requests
Published: 2020-09-18 05:31:21

### The string of cookie looks like this:

```python
ckpf_ppid_safari=a271b829cc244d5c94faae14f73f34df; 
ckpf_ppid_safari=21ebcecf7ab7400483c654469c6b24fb; 
ecos.dt=1600401456420; ecos.dt=1600401456208; 
_em_vt=99882dac-6513-43f6-877f-4f53766e67e5-1749f19f996-
5ca2782f; __gads=ID=a42c30d38b4350e3-
227e540a95c3001b:T=1600399634:RT=1600399634:S=ALNI_MZ1UNYRqcX
wTpGQPoMqq9sATyF6wg; _cb=Z6cFZqJKjWDvIPNE; 
_chartbeat2=.1600397040609.1600399633336.1.Bym-
CVCpzqLyBCabklBCyznkC-mw7l.1; 
atuserid=%7B%22name%22%3A%22atuserid%22%2C%22val%22%3A%22b8d2
9b9f-6075-4bb9-983a-
36e64c3904d2%22%2C%22options%22%3A%7B%22end%22%3A%222021-10-
20T03%3A27%3A12.114Z%22%2C%22path%22%3A%22%2F%22%7D%7D; 
atuserid={%22val%22:%22b8d29b9f-6075-4bb9-983a-
36e64c3904d2%22}; _cc_dc=2; 
_cc_id=b58f5f5f6411e8ab29f7d1086bd0409a; 
ckns_sa_labels_persist={}; 
ckpf_ppid=7b5b127c65d24d939298eb61b7b9a08f; 
ckns_orb_fig_cache=
{%22ad%22:1%2C%22ap%22:0%2C%22ck%22:0%2C%22eu%22:0%2C%22uk%22
:0}; ckns_explicit=1; ckns_policy=111; 
ckns_policy_exp=1631933129982; ckns_sscid=4430f388-f05f-48ff-
9aba-b4837297d7a1; _cb_ls=1; ckns_privacy=july2019
```


### 1. Import essential module:
```python
from http.cookies import SimpleCookie
```
### 2. Assign the loooooooong string to a variable:
```Python
rawdata = 'ckpf_ppid_safari=a271b829cc244d5c94faae14f73f34df; ckpf_ppid_safari=21ebcecf7ab7400483c654469c6b24fb; ecos.dt=1600401456420; ecos.dt=1600401456208; _em_vt=99882dac-6513-43f6-877f-4f53766e67e5-1749f19f996-5ca2782f; __gads=ID=a42c30d38b4350e3-227e540a95c3001b:T=1600399634:RT=1600399634:S=ALNI_MZ1UNYRqcXwTpGQPoMqq9sATyF6wg; _cb=Z6cFZqJKjWDvIPNE; _chartbeat2=.1600397040609.1600399633336.1.Bym-CVCpzqLyBCabklBCyznkC-mw7l.1; atuserid=%7B%22name%22%3A%22atuserid%22%2C%22val%22%3A%22b8d29b9f-6075-4bb9-983a-36e64c3904d2%22%2C%22options%22%3A%7B%22end%22%3A%222021-10-20T03%3A27%3A12.114Z%22%2C%22path%22%3A%22%2F%22%7D%7D; atuserid={%22val%22:%22b8d29b9f-6075-4bb9-983a-36e64c3904d2%22}; _cc_dc=2; _cc_id=b58f5f5f6411e8ab29f7d1086bd0409a; ckns_sa_labels_persist={}; ckpf_ppid=7b5b127c65d24d939298eb61b7b9a08f; ckns_orb_fig_cache={%22ad%22:1%2C%22ap%22:0%2C%22ck%22:0%2C%22eu%22:0%2C%22uk%22:0}; ckns_explicit=1; ckns_policy=111; ckns_policy_exp=1631933129982; ckns_sscid=4430f388-f05f-48ff-9aba-b4837297d7a1; _cb_ls=1; ckns_privacy=july2019'
```
### 3. Convert!
```python
cookie = SimpleCookie()
cookie.load(rawdata)
cookies = {}
for key, morsel in cookie.items():
    cookies[key] = morsel.value
```
### Result:
```python
print(cookies)
# {'ckpf_ppid_safari': '21ebcecf7ab7400483c654469c6b24fb', 'ecos.dt': '1600401456208', '_em_vt': '99882dac-6513-43f6-877f-4f53766e67e5-1749f19f996-5ca2782f', '__gads': 'ID=a42c30d38b4350e3-227e540a95c3001b:T=1600399634:RT=1600399634:S=ALNI_MZ1UNYRqcXwTpGQPoMqq9sATyF6wg', '_cb': 'Z6cFZqJKjWDvIPNE', '_chartbeat2': '.1600397040609.1600399633336.1.Bym-CVCpzqLyBCabklBCyznkC-mw7l.1', 'atuserid': '{%22val%22:%22b8d29b9f-6075-4bb9-983a-36e64c3904d2%22}', '_cc_dc': '2', '_cc_id': 'b58f5f5f6411e8ab29f7d1086bd0409a', 'ckns_sa_labels_persist': '{}', 'ckpf_ppid': '7b5b127c65d24d939298eb61b7b9a08f', 'ckns_orb_fig_cache': '{%22ad%22:1%2C%22ap%22:0%2C%22ck%22:0%2C%22eu%22:0%2C%22uk%22:0}', 'ckns_explicit': '1', 'ckns_policy': '111', 'ckns_policy_exp': '1631933129982', 'ckns_sscid': '4430f388-f05f-48ff-9aba-b4837297d7a1', '_cb_ls': '1', 'ckns_privacy': 'july2019'}
```