9

A legacy database I'm accessing via Django has a table column that stores serialised data in the following string format:

a:5:{i:1;s:4:"1869";i:2;s:4:"1859";i:3;s:4:"1715";i:4;s:1:"0";i:5;s:1:"0";}

Is there any way I can use python/python-library to change it into a list or any other friendly python data type for further processing of individual values?

Note: These values were written to the database via PHP.

sysasa
  • 913
  • 1
  • 9
  • 16

2 Answers2

15

phpserialize:

a port of the serialize and unserialize functions of php to python. This module implements the python serialization interface (eg: provides dumps, loads and similar functions)...

gnat
  • 6,213
  • 108
  • 53
  • 73
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
0

Here's a very rough and incomplete implementation:

def p(f, d):  # parse until
    data = []
    b = f.read(1)
    while b and b != d:
        data.append(b)
        b = f.read(1)
    return b''.join(data)


def parse(f):
    if not hasattr(f, 'read'):
        import io
        try:
            f = io.BytesIO(f)
        except TypeError:
            f = io.BytesIO(f.encode('utf-8'))
    typ = f.read(2)
    if typ == b'i:':
        return int(p(f, b';'))
    if typ == b'd:':
        return float(p(f, b';'))
    if typ == b's:':
        return f.read(int(p(f, b':')) + 3)[1:-2].decode('utf-8')
    if typ == b'a:':
        l = int(p(f, b':'))
        f.read(1)  # {
        items = [(parse(f), parse(f)) for i in range(l)]
        f.read(1)  # }
        return dict(items)
    assert False, typ
Collin Anderson
  • 14,787
  • 6
  • 68
  • 57