Python 使用xlrd迭代所有列
我很难得到一列的所有行。我有这个Python 使用xlrd迭代所有列,python,xlrd,Python,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
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)
但这是返回所有列的数量,而不是返回列的值,如何获取除标题之外的列的所有值?您可以将所有单元格值存储在列表中,例如:
...
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 %}
{第%i行中的%i}
{{i}
{%endfor%}
当前该函数没有返回任何内容。你能更具体地说明你想要函数做什么吗?对不起,我已经编辑了注释,它必须将列的值返回到0,直到与你提供的代码“col”,你的for
循环只迭代一次。好的,谢谢。我如何迭代所有col?列的值由col\u values()提供
。请参阅(实际上是手册)。非常欢迎。干杯,祝你快乐。