If you do not need the serialized text to be human readable, you can use pickle
.
import pickle
s = set([1,2,3])
serialized_s = pickle.dumps(s)
print "serialized:"
print serialized_s
deserialized_s = pickle.loads(serialized_s)
print "deserialized:"
print deserialized_s
Result:
serialized:
c__builtin__
set
p0
((lp1
I1
aI2
aI3
atp2
Rp3
.
deserialized:
set([1, 2, 3])