Home > front end >  select data from excel and save it as a variable
select data from excel and save it as a variable

Time:04-20

I have an excel file which has few rows of string followed by data, I have to select data column wise and save it as a variable. I have tried using openpyxl module, and I have given the code that I am working below. I am able to print the variable nm and count inside loop, but outside loop only one value of the variable (nm, count) will be printed: I need to use these variables to do basic arthamatic operation such as averaging and subtraction later before I plot them. Hence, I need to have them as a variable for my convinence, and I need help in this regard.

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)

CodePudding user response:

The values for nm and counts will be the last value that is iterated in your loop. To access all of the variables, you would have to store them - in a list for example:

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)
  • Related