我从Oracle数据库获取数据。有些数据是clob格式(第8列),所以我必须循环遍历每一行并进行转换。我想追加转换的每一行再次形成原始表。给我带来麻烦的一行是Complete_data = [Complete_data, fixed_data]
import cx_Oracle
# USE THIS CONNECTION STRING FOR PRODUCTION
production_username = ''
production_password = ''
con_string = '%s/%s@host' % (production_username, production_password)
con = cx_Oracle.connect(con_string)
cursor = con.cursor()
querystring = ("Select * from SalesDatabase")
cursor.execute(querystring)
data = cursor.fetchall()
#loop through and convert clobs to readable content
for currentrow in data:
Product = currentrow[8].read()
fixed_data = ([currentrow[0], currentrow[1], currentrow[2], currentrow[3], currentrow[4], currentrow[5], currentrow[6], currentrow[7], Product, currentrow[9]])
Complete_data = [Complete_data, fixed_data]
con.close()
print Complete_data
答案 0 :(得分:1)
填充列表的传统方法是创建一个开头的空列表,并在循环中创建append
项。
Complete_data = []
for currentrow in data:
Product = currentrow[8].read()
fixed_data = ([currentrow[0], currentrow[1], currentrow[2], currentrow[3], currentrow[4], currentrow[5], currentrow[6], currentrow[7], Product, currentrow[9]])
Complete_data.append(fixed_data)