Python中文
首页
教程
问答
标签
搜索
登录
注册
元组索引必须是整数或切片,而不是s
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我在使用mySQL时遇到了这个问题。我的结果是<code>[{'email': 'bernie@shrimp.com'}]</code>,但当我做<code>email = results[0]['email']</code>时,我得到了<code>TypeError: tuple indices must be integers or slices, not str</code></p> <p>问题是当我在本地运行这个程序时,它工作得很好。我怎么得到<code>bernie@shrimp.com</code>?</p> <p>用户是一个表</p> <p>代码:</p> <pre><code>cursor.execute('SELECT email FROM users WHERE username = %s', [attempted_username]) email_dict = cursor.fetchall() print(email_dict) session['email'] = email_dict[0]['email'] </code></pre> <p>控制台:</p> <pre><code>[{'email': 'bernie@shrimp.com'}] </code></pre>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p><code>fetchall</code>返回元组列表。您需要按列的序号而不是名称来访问它:</p> <pre><code>session['email'] = email_dict[0][0] </code></pre>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
如何用if条件捕获函数返回值
1 回答
如何用if语句判断列表中是否存在该索引?
7 回答
如何用if语句向量化numpy数组中的最大值?
4 回答
如何用IF语句有条件地保存零碎的结果?
8 回答
如何用if语句测试异常对象?
5 回答
如何用IF语句编写二元函数
5 回答
如何用igraph在python中创建顶点权重的图?
7 回答
如何用ijson和python解析json
8 回答
如何用iloc求子矩阵
8 回答
如何用Imagemagick或PIL绘制高质量的图像笔划(边框)?
4 回答
如何用importlib在python中动态导入模块?
1 回答
如何用import语句重写python内置函数?
2 回答
如何用imshow混合裁剪的强度并显示正确的混合强度?
5 回答
如何用in dictionary解析havin dictionary中的json文件
10 回答
如何用in-Django URL替换%20
2 回答
如何用in\op正确构造查询
7 回答
如何用inbuild对象替换文件
2 回答
如何用inheritan类实现flask restful
1 回答
如何用intersphinx正确地编写对外部文档的交叉引用?
5 回答
如何用int修改LpVariable?
5 回答