3

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)

asked Mar 10, 2011 at 12:01
2
  • 1
    You can use database normalization. Commented 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. Commented Mar 10, 2011 at 12:06

4 Answers 4

5

You can try to use json to turn it into a string, like this:

import json
v = [1, 2, 4]
s = json.dumps(v)
answered Mar 10, 2011 at 12:04
Sign up to request clarification or add additional context in comments.

2 Comments

oh I thought that json was only for java... I try this now, but how can I unserialize? thanks friend!!
@igferne: There's volumes upon volumes of documentation at the Python website.
1

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.

answered Mar 10, 2011 at 12:09

Comments

0

Doesn't MySQL let you store these pickled bytes in a blob?

answered Mar 10, 2011 at 12:09

Comments

0

... or perhaps just

str([1,2,4])

(if you dont necessarily need the data in json-format)

answered Mar 10, 2011 at 12:12

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.