'tuple'对象不支持数组上的项目分配

问题描述 投票:0回答:1

当我尝试更改其中一个单元格时,我想从我的数据库中进行选择

这是我的代码:

command = "select desc ,city,datetime,loc from mytable'"
cursor.execute(command)
result = cursor.fetchall()
i = 0
for x in result:
   myary.append(result[i])
   i= i+1

my_list = list(myary)
for y in range(0,len(myary)):
    sip = myary[y][0].split("/")
    my_list [y][0]=sip[0]
myary = tuple(my_list)

输出:

'tuple' object does not support item assignment

谁能告诉我出了什么问题吗?

python mysql tuples
1个回答
0
投票

tuples
在Python中是不可变的。在您尝试修改的代码中。要修复它,您可以将其转换为
list
。像这样的东西:

command = "select desc ,city,datetime,loc from mytable'"
cursor.execute(command)
result = cursor.fetchall()

my_list = [list(x) for x in result]  # Convert each tuple to a list

for y in range(0,len(my_list)):
    sip = my_list[y][0].split("/")
    my_list[y][0]=sip[0]

myary = tuple(my_list)  # Convert the list of lists back to a tuple of tuples
© www.soinside.com 2019 - 2024. All rights reserved.