在Python中将元组转换为列表以便修改

0 投票
1 回答
751 浏览
提问于 2025-04-18 18:01

我正在查询一个Oracle数据库,需要对其中一列数据进行特别处理,这列数据是一个大文本(clob)。我可以用.read()来读取这个大文本。我想把实际的值写回我的数组。因为这是一个元组(tuple),所以我必须先把它转换成列表(list),写入值后再转换回元组。可是我遇到了一个错误信息:TypeError: 'tuple' object does not support item assignment

我的代码:

import cx_Oracle

# USE THIS CONNECTION STRING FOR PRODUCTION
production_username = 'username'
production_password = 'password'

con_string = '%s/%s@hostname:port/orcl' % (production_username, production_password)
con = cx_Oracle.connect(con_string)
cursor = con.cursor()
querystring = ("Select ID, Description from Table")
cursor.execute(querystring)
data = cursor.fetchall()

for currentrow in range(1, len(data)): 
     description= data[currentrow][1].read()
    data = list(data)
    data[currentrow][1] = description
    data = tuple(data)
con.close()
print data

1 个回答

1

试试这个方法

for currentrow in data : 
     description= currentrow[1].read()
     tupled_data= tuple([currentrow[0],description])


print tupled_data

撰写回答