Home > Net >  How to Create cookie data from dictionary - Python
How to Create cookie data from dictionary - Python

Time:09-28

{'full_name': 'Test User', 'sid': '09a5de5792d5ad465d4bd0ab0986618e834f09d6af79dba608420efe', 'system_user': 'yes', 'user_id': '[email protected]', 'user_image': ''}

i want to convert the above dictionary format into below cookie format

'full_name=Test User; sid=7f25bb3a8129f4369c22f43bde382328089df64af77bd0200f9550d9; system_user=yes; [email protected]; user_image='

CodePudding user response:

You can do this using .join twice with a list comprehension, once to join the dict items with =, and another to join the list with ; (note the space)

d = {'full_name': 'Test User', 'sid': '09a5de5792d5ad465d4bd0ab0986618e834f09d6af79dba608420efe', 'system_user': 'yes', 'user_id': '[email protected]', 'user_image': ''}

"; ".join(["=".join(x) for x in d.items()])

'full_name=Test User; sid=09a5de5792d5ad465d4bd0ab0986618e834f09d6af79dba608420efe; system_user=yes; [email protected]; user_image='
  • Related