Array serialization in Python

I am trying to serialize an array in python to insert it into a MySQL database ... I am trying to use the pickle.dump () method, but it returns a byte ... what can I use? thank!!

(I work on python 3)

+3
source share
4 answers

You can try using json to turn it into a string, for example:

import json

v = [1, 2, 4]
s = json.dumps(v)
+5
source

Pickle is binary serialization, so you get a byte string.

Pros:

  • more compact
  • can express most Python objects.

Con's:

  • bytes may be harder to process
  • Python only.

JSON , Python. ASCII, . con , , , dicts. , datetime .

+1

Doesn't MySQL allow you to store these pickled bytes in blob ?

0
source

... or maybe just

str([1,2,4])

(if you do not need data in json format)

0
source

All Articles