0

I am trying to convert the type of the data fetched from the database into the binary type. The data fetched from the database is in the form of a list. Below is the sample code:

def WriteData(data):

    jsonData = json.dumps(data)
    binaryData = ' '.join(format(ord(letter), 'b') for letter in jsonData)
    print(type(binaryData))
    filePointer = io.BytesIO(binaryData)

The output of the code is:

<class 'str'>

Swifty
  • 839
  • 2
  • 15
  • 40
Dyuti Jain
  • 11
  • 1
  • 2
  • 3
    This is really unclear. Why do you need to do this? What is the required output? What is wrong with the code you have? – Daniel Roseman Nov 21 '17 at 13:21
  • `' '.join()` will create a string. – Reti43 Nov 21 '17 at 13:21
  • `binaryData` is a string. So it's perfectly correct that `print(type(binaryData))` prints ``. What were you expecting it to print? And what are you _really_ trying to do? Converting text codepoints to space-separated variable length strings of zeros & ones doesn't seem very useful. – PM 2Ring Nov 21 '17 at 13:24
  • If you must do this, why not `binaryData = bytearray(jsonData, encoding='ascii')`? – timgeb Nov 21 '17 at 13:24

1 Answers1

0

I am thinking since BytesIO takes bytes as a parameter you want to convert the json dumped data to bytes. For this you can just use encode() method

def WriteData(data):

    jsonData = json.dumps(data)
    binaryData = jsonData.encode()
    print(type(binaryData))
    filePointer = io.BytesIO(binaryData)
daemon24
  • 1,388
  • 1
  • 11
  • 23