简体   繁体   中英

How to read each column value from uploaded excel (xls or xlsx) file in python?

I am uploading an excel sheet (.xls or .xlsx format). After uploading the file I want to read value of each column.

My html is

<form id = "ListForm" name = "ListForm" action = "" method = 'POST' enctype="multipart/form-data">
<table>
<tr>
<td>PM List</td>
<td><input type="file" name="file_pm" id="file_pm" ></td>
</tr>   
<tr><td><input type="submit" value="Upload" name="pmUpload" id="pmUpload" class="button"></td></tr>       
</table>
</form>

Python code is

def pmUpload(request):
    data = request.FILES['file_pm']
    # here I want to iterate through each column & rows in excel.

Can anybody suggest the method to iterate

You can use csv python library.

file = open('file.xls')
csvreader = csv.reader(file, delimiter=',')
for r in csvreader: # iterates each row
    print r         # r is a list of column of that row.  

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM