Q:

Pythonic way to parse preflib Orders with Ties files

I'm working with data from preflib.org, especially with the "Orders with Ties" format. The format looks (somewhat) like this:
1,2,{3,4,5},6
2,{3,6,4},1,5
{2,3},{4,6},{1,5}
...

I need to parse every line of these files into a list of tuples, where every tuple contains one "equivalence class" of choices. In this example:
1,2,{3,4,5},6 -> [(1,), (2,), (3,4,5), (6,)]
2,{3,6,4},1,5 -> [(2,), (3,6,4), (1), (5,)]
{2,3},{4,6},{1,5} -> [(2,3), (4,6), (1,5)]

Currently this is solved with ugly string manipulations etc. and I am pretty sure there is something more pythonic to solve this (preferably with builtins only).
EDIT: What I do currently (very hacky and ugly ...):
s = "1,2,{3,4,5},6"
classes = []
equiv_cls = None
for token in s.split(","):
    if token.startswith("{"):
        equiv_cls = [token[1:]]
    elif token.endswith("}"):
        equiv_cls.append(token[:-1])
        classes.append(tuple(equiv_cls))
        equiv_cls = None
    elif equiv_cls is not None:
        equiv_cls.append(token)
    else:
        classes.append(tuple(token))

A:

You can use ast.literal_eval with some str.replace calls:
>>> from ast import literal_eval
>>> s = '1,2,{3,4,5},6'
>>> [x if isinstance(x, tuple) else (x,) for x 
                         in literal_eval(s.replace('{', '(').replace('}', ')'))]
[(1,), (2,), (3, 4, 5), (6,)]

As @Martijn Pieters suggested you can replace the two str.replace calls with a single str.translate call:
>>> from string import maketrans
>>> table = maketrans('{}', '()')
>>> [x if isinstance(x, tuple) else (x,) for x in literal_eval(s.translate(table))]
[(1,), (2,), (3, 4, 5), (6,)]

In Python 3 you won't need any str.replace or str.translate calls calls, it fails in Python 2.7 and here is the related bug:
>>> [tuple(x) if isinstance(x, set) else (x,) for x in literal_eval(s)]
[(1,), (2,), (3, 4, 5), (6,)]

