Iterating all columns with xlrd

情到浓时终转凉″ 提交于 2020-01-24 18:58:06

问题


Im having troubles getting all the row of a column. i have this

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)

but this is returning the number of the all columns, does not return the values of the columns, how can i get all the values of a column excepting the header?


回答1:


You could store all the cell values in a list, e.g.:

    ... 
    row = []
    for col in range(sheet.ncols):
        row.append( sheet.cell_value(0,col) )
    return render_to_response('your_template.html', {'row': row})

And in your template something like:

{% for i in row %}
    <p>{{ i }}</p>
{% endfor %}


来源:https://stackoverflow.com/questions/26615660/iterating-all-columns-with-xlrd

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!