2017-08-22 27 views
-1

有人可以幫助我用python打開一個excel文件,並選擇一個帶數字的特定列,我想將這些數字一個接一個地保存在變量中。重點是保存號碼並將其發佈到網站上。打開一個excel並選擇一個特定的列

回答

0

您可以使用xlrd模塊讀取excel文件。

我已經考慮列是0,也表0

import xlrd 
column = 0 
worksheet = xlrd.open_workbook('test_file.xls') 
handle_sheet_0 = worksheet.sheet_by_index(0) 
data = [] 
for row in range(handle_sheet_0.nrows): 
     data.append(handle_sheet_0.cell(row, column).value) 
+0

是否單獨打開excel文件? – JSN

+0

是的。 xlrd.open_workbook('test_file.xls')行將採用** test_file.xls **文件。 – Nirmi

+0

但測試文件的路徑.xls沒有提到對 – JSN

0

可以使用xlrd包

import xlrd 
file_location = "C:/Users/Desktop/my.xlsx" 
wrk_book = xlrd.open_workbook(file_location) # opening the file 
sheet = wrk_book.sheet_by_index(0)   # selecting the sheet number 
cell = sheet.cell_value(0, 0)      # Selecting a particular cell 
col = 0          # If you want to select column A 
values = list([]) 
for row in range(sheet.nrows): 
    print(sheet.cell_value(row, col)) 
    values.append(sheet.cell_value(row, col)) # you can store the value in a list 
+0

print sheet.cell(row,col).value 在這個命令我想用變量i替換行,只需將行替換爲i不起作用。有什麼建議麼 ? – JSN

相關問題