Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用xlrd迭代所有列_Python_Xlrd - Fatal编程技术网

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()提供
。请参阅(实际上是手册)。非常欢迎。干杯,祝你快乐。