How would I convert string of namedtuples to a list?
The problem is I have to store a list of namedtuples in a column in SQLite, which (obviously) doesn't support the format. I thought of just converting it into a string. However, since my tuple is a namedtuple, I don't know how to go from the string to list again.
>>> Point = namedtuple("Point", "x y", verbose = False)
>>> p = Point(3, 5)
>>> points = []
>>> points.append(Point(4, 7))
>>> points.append(Point(8, 9))
>>> points.append(p)
>>> p.x
3
>>> print points
[Point(x=4, y=7), Point(x=8, y=9), Point(x=3, y=5)]
My list of named tuples is something like this^^^^, but it has 6 arguments instead of the 2 shown above. Edit - the arguments are booleans, ints, and strings.
I tried mapping, but i got the following error:
>>> string = str(points)
>>> l = string.strip("[]")
>>> p = map(Point._make, l.split(", "))
Traceback (most recent call last):
File "<pyshell#31>", line 1, in <module>
p = map(Point._make, l.split(", "))
File "<string>", line 17, in _make
TypeError: Expected 2 arguments, got 9
I'm open to other simpler ways to do this.