在Python循环中向列表添加行
我正在从一个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
1 个回答
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)