title: Python: Convert dictionary to bytes
body: <p>I'm trying to convert a dictionary to bytes but facing issues in converting it to a correct format. </p>
  
  <p>First, I'm trying to map an dictionary with an custom schema. Schema is defined as follows -</p>
  
  <pre><code>class User:
      def __init__(self, name=None, code=None):
          self.name = name
          self.code = code
  
  class UserSchema:
      name = fields.Str()
      code = fields.Str()
  
  @post_load
   def create_userself, data):
      return User(**data)
  </code></pre>
  
  <p>My Dictionary structure is as follows-</p>
  
  <pre><code>user_dict = {'name': 'dinesh', 'code': 'dr-01'} 
  </code></pre>
  
  <p>I'm trying to map the dictionary to User schema with the below code</p>
  
  <pre><code>schema = UserSchema(partial=True)
  user = schema.loads(user_dict).data
  </code></pre>
  
  <p>While doing, <strong>schema.loads</strong> expects the input to be str, bytes or bytearray. Below are the steps that I followed to convert dictionary to Bytes</p>
  
  <pre><code>import json
  user_encode_data = json.dumps(user_dict).encode('utf-8')
  print(user_encode_data)
  </code></pre>
  
  <p><strong>Output:</strong></p>
  
  <pre><code>b'{"name ": "dinesh", "code ": "dr-01"}
  </code></pre>
  
  <p>If I try to map with the schema I'm not getting the required schema object. But, if I have the output in the format given below I can able to get the correct schema object.</p>
  
  <pre><code>b'{\n  "name": "dinesh",\n  "code": "dr-01"}\n'
  </code></pre>
  
  <p>Any suggestions how can I convert a dictionary to Bytes?</p>