名前付きタプルの文字列をリストに変換するにはどうすればよいですか?
問題は、名前付きタプルのリストを SQLite の列に格納する必要があることです。これは (明らかに) 形式をサポートしていません。文字列に変換するだけだと思いました。ただし、私のタプルは名前付きタプルなので、文字列からリストに再度移動する方法がわかりません。
>>> 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)]
私の名前付きタプルのリストはこのようなものです^^^^ ですが、上記の 2 つではなく 6 つの引数があります。編集 - 引数はブール値、int、および文字列です。
マッピングを試みましたが、次のエラーが発生しました。
>>> 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
これを行うための他のより簡単な方法を受け入れます。