1. 程式人生 > >python巢狀列表

python巢狀列表

轉自我的部落格園:http://www.cnblogs.com/vickey-wu/p/6679482.html

從excel讀取一行資訊新增到一個臨時列表,最後將所有行的列表新增到一個大列表。


原始碼:

import xlrd,re
class Info():
def read_info(self):
data = xlrd.open_workbook('tmp_info.xlsx')
table = data.sheets()[0]
# row_val = table.row_values(0)
info_list = []
tmp_list = []
for i in range(1,table.nrows):
tmp = table.row_values(i)
for j in range(3):
val = re.sub(' ','',tmp[j].encode('utf-8'))
val = re.sub('#','',val)
if len(tmp[2]) > 50:
val = re.sub('_','',val)
tmp_list.append(val)
info_list.append(tmp_list)
tmp_list = [] # must clear it
return info_list
t = Info()
print t.read_info()