我有一个数据集,其中有12列是来自数据集的一些样本数据。
[['1,1412,,Correy',
'Edmed,00:06:53,35-39,1,Male,1,WOOLLOONGABBA,00:06:54,07:15:33,07:22:27'],
['1000,3060,,Nathan',
'Hankinson,00:17:33,30-34,57,Male,879,COORPAROO,00:17:34,07:58:27,08:16:01'],
['1000,4297,,Jason',
'Taylor,00:17:33,40-44,107,Male,879,GEEBUNG,00:17:34,07:42:08,07:59:41'],
['1002,4014,,Thomas',
'Collins,00:17:34,14-19,20,Male,881,JINDALEE,00:17:35,08:00:04,08:17:39'],
['1003,4146,,Avis',
'Houlihan,00:17:35,60-69,8,Female,117,CARINDALE,00:17:36,08:00:34,08:18:09'],
['1003,4426,,Greg',
'Burt,00:17:35,60-69,87,Male,882,BRISBANE,00:17:36,07:57:42,08:15:17'],
['1005,1352,,Amanda',
'Wilson,00:17:38,45-49,25,Female,118,CASINO,00:17:39,07:37:43,07:55:21'],...
我使用的是12列,但是当我将此代码放入创建表时,它会让我的索引超出范围
html_content = "<html><body><table><tr><td>" + bike_list[0][0] + "</td><td>" + bike_list[0][1] + "</td><td>" + bike_list[0][2] + "</td><td>" + bike_list[0][3] + "</td><td>" + bike_list[0][1] + "</td><td>" + bike_list[0][4] + "</td><td>" + bike_list[0][5] + "</td><td>" + bike_list[0][6] + "</td><td>" + bike_list[0][7] + "</td><td>" + bike_list[0][8] + "</td><td>" + bike_list[0][1] + "</td><td>" + bike_list[0][9] + "</td><td>" + bike_list[0][10] + "</td><td>" + bike_list[0][11] + "</td></tr>"
i=1
while i < len(bike_list):
html_content += "<tr><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td></tr>".format(bike_list[i][0],bike_list[i][1],bike_list[i][2],bike_list[i][3],bike_list[i][4],bike_list[i][5],bike_list[i][6],bike_list[i][7],bike_list[i][8],bike_list[i][9],bike_list[i][10],bike_list[i][11])
i+=1
html_content += "</table></body></html>"
为什么会这样?
答案 0 :(得分:2)
因为列表在Python中是0索引的。
但这不是迭代列表的方法。为此,我们有for
个循环。
另请注意,您可以使用*
运算符一次传递整个子列表。
for elem in bike_list:
html_content += "<tr><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td></tr>".format(*Elem)