i have an question about json and text files in python.
I have an .txt file with cookies, i have about 11 of them but i will show only 2. I need to convert them to .json file so i could put them in request headers of an post request.
Here are the cookies in .txt
Set-Cookie3: X-APPLE-WEBAUTH-PCS-Mail="\"TGlzdEFwcGw6MTpBcHBsOjE6AZInfMKt5IWY2iftwASOV2SUGxMMn0ofLN6m6Tl5DsSBDGXPI2Uhfb7IkYx4GWgET2U5FJdBpxnvF1wGPEV2zKGC7UpT2CoDkBSZAGtIHzkmI6zjqXxGS/ObRGVSX4BuUaP6A2Tgt9CKVHzVZG8O hA8dDxdCy513OkmNYLNRT8a5M7VnDGyqg==\""; path="/"; domain=".icloud.com"; path_spec; domain_dot; secure; expires="2022-06-14 13:29:15Z"; HttpOnly=None; version=0
Set-Cookie4: X-APPLE-WEBAUTH-PCS-News="\"TGlzdEFwcGw6MTpBcHBsOjE6ARKs9FWrQDcrZGUYbHPO2e7Jux6aRGWLAZt4vjHFRLgqERXDD51hgI8ADM/A3SViCTgpnEfThOKn rJ4rXRlkyQqq3ZPRdlieQI6g/FYs8RBm5Y0fCcFk5RAHq2VQIibURXAttOjyvVo3JHNuJ38zOmGaytYnSujeI6qFNrtep0YV7Ptq6lKcg==\""; path="/"; domain=".icloud.com"; path_spec; domain_dot; secure; expires="2022-06-14 13:29:15Z"; HttpOnly=None; version=0
and i want them to look like this
{
'Set-Cookie3': 'X-APPLE-WEBAUTH-PCS-Mail="\"TGlzdEFwcGw6MTpBcHBsOjE6AZInfMKt5IWY2iftwASOV2SUGxMMn0ofLN6m6Tl5DsSBDGXPI2Uhfb7IkYx4GWgET2U5FJdBpxnvF1wGPEV2zKGC7UpT2CoDkBSZAGtIHzkmI6zjqXxGS/ObRGVSX4BuUaP6A2Tgt9CKVHzVZG8O hA8dDxdCy513OkmNYLNRT8a5M7VnDGyqg==\""; path="/"; domain=".icloud.com"; path_spec; domain_dot; secure; expires="2022-06-14 13:29:15Z"; HttpOnly=None; version=0',
'Set-Cookie4': 'X-APPLE-WEBAUTH-PCS-News="\"TGlzdEFwcGw6MTpBcHBsOjE6ARKs9FWrQDcrZGUYbHPO2e7Jux6aRGWLAZt4vjHFRLgqERXDD51hgI8ADM/A3SViCTgpnEfThOKn rJ4rXRlkyQqq3ZPRdlieQI6g/FYs8RBm5Y0fCcFk5RAHq2VQIibURXAttOjyvVo3JHNuJ38zOmGaytYnSujeI6qFNrtep0YV7Ptq6lKcg==\""; path="/"; domain=".icloud.com"; path_spec; domain_dot; secure; expires="2022-06-14 13:29:15Z"; HttpOnly=None; version=0'
}
the brackets dont have to look like that, they can look like this for example. This will be better, also very important is the comma
{'cookie': 'value', 'cookie': 'value'}
i tried many solutions but many of them didnt work, for ex. thought each cookie is an different group so they have different brackets, or just quoting the colon. Any help will be appriciated:)) Thank you!
CodePudding user response:
Something like that should work:
with open("cookies.txt", "r") as f: # don't forget to change the filename
txt = f.readlines()
d = {line.split(":", maxsplit=1)[0]: line.split(":", maxsplit=1)[1].lstrip() for line in txt}
Output:
>>> print(d)
{'Set-Cookie3': 'X-APPLE-WEBAUTH-PCS-Mail="\\"TGlzdEFwcGw6MTpBcHBsOjE6AZInfMKt5IWY2iftwASOV2SUGxMMn0ofLN6m6Tl5DsSBDGXPI2Uhfb7IkYx4GWgET2U5FJdBpxnvF1wGPEV2zKGC7UpT2CoDkBSZAGtIHzkmI6zjqXxGS/ObRGVSX4BuUaP6A2Tgt9CKVHzVZG8O hA8dDxdCy513OkmNYLNRT8a5M7VnDGyqg==\\""; path="/"; domain=".icloud.com"; path_spec; domain_dot; secure; expires="2022-06-14 13:29:15Z"; HttpOnly=None; version=0\n', 'Set-Cookie4': 'X-APPLE-WEBAUTH-PCS-News="\\"TGlzdEFwcGw6MTpBcHBsOjE6ARKs9FWrQDcrZGUYbHPO2e7Jux6aRGWLAZt4vjHFRLgqERXDD51hgI8ADM/A3SViCTgpnEfThOKn rJ4rXRlkyQqq3ZPRdlieQI6g/FYs8RBm5Y0fCcFk5RAHq2VQIibURXAttOjyvVo3JHNuJ38zOmGaytYnSujeI6qFNrtep0YV7Ptq6lKcg==\\""; path="/"; domain=".icloud.com"; path_spec; domain_dot; secure; expires="2022-06-14 13:29:15Z"; HttpOnly=None; version=0'}
Just be careful, as others have noted, there can't be identical keys in a dict
.