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`}