访问tup中的值

2024-09-30 08:17:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要一些帮助来访问元组中的值或条目。你知道吗

考虑以下数据:

('{"email": "test@example.com"}',)
<type 'tuple'>
{"email": "test@example.com"}
<type 'str'>

首先它基本上是一个元组。你知道吗

经过进一步处理,它正在转换为“str:。你知道吗

你可以看看下面的代码。你知道吗

所以这里我需要得到“email”的值。你知道吗

我尝试了以下代码

rows = cur.fetchall()

for row in rows:
    print  row
    print type(row)

    for rowitems in row:

            print rowitems
            print type(rowitems)

有人让我的方式一样。你知道吗


Tags: 代码intestcomforexampleemailtype
1条回答
网友
1楼 · 发布于 2024-09-30 08:17:59

可以使用^{}将字符串解析为JSON。你知道吗

示例

>>> import json
>>> a_tuple = ('{"email": "test@example.com"}',)
>>> [ json.loads(value)['email'] for value in a_tuple ]
[u'test@example.com']

这将为您提供来自a_tupleemails列表

相关问题 更多 >

    热门问题