我有一個 excel 檔案,其中有幾行字串,后跟資料,我必須明智地選擇資料列并將其保存為變數。我嘗試過使用 openpyxl 模塊,并給出了我在下面作業的代碼。我可以在回圈內列印變數 nm 和 count,但在回圈外,只會列印變數 (nm, count) 的一個值:我需要使用這些變數來執行基本的藝術運算,例如稍后在我之前的平均和減法繪制它們。因此,我需要將它們作為我的說服力的變數,在這方面我需要幫助。
wb = openpyxl.load_workbook(filename='C:/Users/experiment 1//S01/S_D_Sp1_S01.xlsx')
a_sheet_names = wb.get_sheet_names()
print(a_sheet_names)
ws=wb.active
lamda=ws['A6:A17']
intensity=ws['B6:B17']
#loop
for x in lamda:
for nm in x:
print(nm.value)
for y in intensity:
for counts in y:
print(counts.value)
print (nm,counts)
uj5u.com熱心網友回復:
nm和的值counts將是回圈中迭代的最后一個值。要訪問所有變數,您必須將它們存盤在一個串列中,例如:
nm_all = []
counts_all = []
for x in lamda:
for nm in x:
nm_all.append(nm)
for y in intensity:
for counts in y:
counts_all.append(counts)
# now you can access all values
print(nm_all)
print(counts_all)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/460300.html
上一篇:更新類變數的值-C
