Skip to main content
 首页 » 编程设计

python之要列出的命名元组字符串

2025年05月04日26bluestorm

如何将命名元组字符串转换为列表?

问题是我必须在 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)] 

我的命名元组列表是这样的^^^^,但它有 6 个参数而不是上面显示的 2 个。编辑 - 参数是 bool 值、整数和字符串。

我试过映射,但出现以下错误:

>>> 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 

我对其他更简单的方法持开放态度。

请您参考如下方法:

最终,如何做到这一点可能是个人喜好问题。

JSON

Json 可以很好地使用,因为与 pickle 不同,它可以在 python 之外使用。您的对象以广泛支持且易于重新利用的格式进行序列化。

>>> import json  # simple json is better bit I didn't want to force an install 
>>> from collections import namedtuple 
>>> Point = namedtuple("Point", "x y", verbose = False) 
>>> p = Point(3,4) 
>>> json.dumps(p._asdict()) 
'{"x": 3, "y": 4}' 
>>> s = json.dumps(p._asdict()) 
>>> json.loads(s)  # not there yet cause thisis a dict 
{u'y': 4, u'x': 3}   # but it is a dict that can create a Point 
>>> Point(**json.loads(s)) 
Point(x=3, y=4)     

泡菜

除非您定义属性状态,否则 pickle 将不起作用(参见 __getstate__ in the docs )。 这是加载阶段的“更好”,从上面开始:

import pickle 
 
# Point.__getstate__=lambda self: self._asdict() # not needed as per @simon's comment thx simon 
>>> pickle.dumps(p) 
"ccopy_reg\n_reconstructor\np0\n(c__main__\nPoint\np1\nc__builtin__\ntuple\np2\n(I3\nI4\ntp3\ntp4\nRp5\nccollections\nOrderedDict\np6\n((lp7\n(lp8\nS'x'\np9\naI3\naa(lp10\nS'y'\np11\naI4\naatp12\nRp13\nb." 
s = pickle.dumps(p) 
>>> pickle.loads(s) 
Point(x=3, y=4) 

评估

我不鼓励使用 eval 或 exec。如果你真的沿着那条路走下去,请查看ast.literal_eval()并查看一些与 SO 相关的答案,例如 safety of python eval