I ́m trying to serialize an array in python to insert it on a database of MySQL... I try with pickle.dump() method but it returns byte... what can I use?? thanks!!
(I ́m working in python 3)
-
1You can use database normalization.Ignacio Vazquez-Abrams– Ignacio Vazquez-Abrams2011年03月10日 12:02:53 +00:00Commented Mar 10, 2011 at 12:02
-
Python 3 is not yet supported for daily development and it is not be a good idea to be already using it is production projects.tiagoboldt– tiagoboldt2011年03月10日 12:06:09 +00:00Commented Mar 10, 2011 at 12:06
4 Answers 4
You can try to use json to turn it into a string, like this:
import json
v = [1, 2, 4]
s = json.dumps(v)
2 Comments
Pickle is a binary serialization, that's why you get a byte string.
Pros:
- more compact
- can express most of Python's objects.
Con's:
- bytes can be harder to handle
- Python only.
JSON is more universal, so you're not tied to reading data with Python. It's also mostly ASCII, so it's easier to handle. the con is that it's limited to numbers, strings, arrays and dicts. usually enough, but even datetimes have to be converted to string representation before encoding.
Comments
Doesn't MySQL let you store these pickled bytes in a blob?
Comments
... or perhaps just
str([1,2,4])
(if you dont necessarily need the data in json-format)