Python字符串索引必须是整数而不是str

Python字符串索引必须是整数而不是str,python,python-2.7,Python,Python 2.7,这是我的代码: dbcur.execute('select girismuzik from users where name = ?', [username]) rrf = dbcur.fetchone() girismuzik = rrf self.sendData("\x1A" + "\x0C" [girismuzik]) 但这是一个错误: string indices must be integers not str 您正在尝试在此处分割字符串: self.sendData("\x1A

这是我的代码:

dbcur.execute('select girismuzik from users where name = ?', [username])
rrf = dbcur.fetchone()
girismuzik = rrf
self.sendData("\x1A" + "\x0C" [girismuzik])
但这是一个错误:

string indices must be integers not str

您正在尝试在此处分割字符串:

self.sendData("\x1A" + "\x0C" [girismuzik])
girismuzik
指的是字符串值

也许你忘了一个逗号

self.sendData("\x1A" + "\x0C", [girismuzik])

如果没有stacktrace和代码的其余部分,很难说出更多信息。但是,

self.sendData("\x1A" + "\x0C" [girismuzik]) 
这就是问题所在

具体来说:
“\x0C”[girismuzik]

这是一个字符串,然后是一个索引。(因为您可能忘记了一个
)。它将转换为
“string”[x]
,如果
x
不是int,则为错误。

A
游标。fetchone()
可能会从查询返回一个结果行的元组(其中有一列)

你可能想要的东西更像:

dbcur.execute('select girismuzik from users where name = ?', [username])
rrf = dbcur.fetchone()
girismuzik = rrf[0]
self.sendData("\x1A" + "\x0C" + girismuzik)

错误到底是在哪里产生的?可能添加StackTrace您正在对长度为1的常量字符串进行字符串索引操作。如果这是您想要做的,您可以完全删除索引访问。