Change language

Python | Convert key byteString: a pair of dictionary values ​​to a string

| |

Examples :

  Input:  {b’EmplId’: b’12345’, b’Name’: b’Paras’, b ’Company’: b’Cyware’}  Output:  {’ EmplId’: ’12345’,’ Name’: ’Paras’,’ Company’: ’Cyware’}  Input:  {b’Key1’: b’Geeks’, b’Key2’: b’For’, b’Key3’: b’Geek’}  Output:  {’Key1’:’ Geeks’ , ’Key2’:’ For’, ’Key3’:’ Geek’} 

Method # 1: dictionary comprehension

# Python code to convert ByteString key: value
# a pair of dictionary to string.

 
# Initializing dictionary

x = {b ’EmplId’ : b ’12345’ , b ’ Name’ : b ’Paras’ , b ’Company’ : b ’Cyware’ }

  
# Conversion

x = {y.decode ( ’ ascii’ ): x.get (y) .decode ( ’ascii’ ) for y in x.keys ()}

  
# printing the converted dictionary

print (x)

Output:

 {’Name’:’ Paras’, ’EmplId’:’ 12345’, ’Company’:’ Cyware’} 

Method # 2: c by iterating key and values ​​

# Python code to convert ByteString key: value
# a pair of dictionary per string.

 
# Initializing dictionary

x = {b ’EmplId’ : b ’ 12345’ , b ’Name’ : b ’ Paras’ , b ’Company’ : b ’ Cyware’ }

 
# Initialize an empty dictionary

y = {}

  
# Conversion

for key, value in x.items ():

y [key.decode ( "utf-8" )] = value.decode ( " utf-8 " )

 
# print converted dictionary

print (y)

Exit:

 {’Co mpany’: ’Cyware’,’ Name’: ’Paras’,’ EmplId’: ’12345’} 

Python | Convert key byteString: a pair of dictionary values ​​to a string __dict__: Questions

Python | Convert key byteString: a pair of dictionary values ​​to a string Python functions: Questions

Shop

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method