使用xlrd遍历所有列
我在获取某一列的所有行时遇到了问题。我有这个
def excel(request):
file_location = "proyectos.xls"
workbook = xlrd.open_workbook(file_location)
sheet = workbook.sheet_by_index(0)
for col in range(sheet.ncols):
data = sheet.cell_value(0,col)
return HttpResponse (data)
但是这个代码返回的是所有列的数量,并没有返回列的具体值。我该怎么做才能获取某一列的所有值,而不包括表头呢?
1 个回答
2
你可以把所有单元格的值存放在一个列表里,比如:
...
row = []
for col in range(sheet.ncols):
row.append( sheet.cell_value(0,col) )
return render_to_response('your_template.html', {'row': row})
然后在你的模板中可以用类似这样的方式:
{% for i in row %}
<p>{{ i }}</p>
{% endfor %}