2014-08-27 39 views
1

我從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/%[email protected]' % (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

來填充列表的傳統方法,是創建一個空列表入手,並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) 
+0

非常感謝。 – user2242044 2014-08-27 17:49:04

相關問題