jayprod - 1 month ago 4

Python Question

Sup folks, well I have a big problem. First of all I'm new in python (atm 3.x). I need a help to figure out, how to pick correct data exactly for calculations (Sorry for my english). For eg.:

I have a excel file with data:

My excel data looks like this

I can count this manually for e.g

`book = openpyxl.load_workbook('angles.xls')`

sheet = book.get_sheet_by_name('Sheet1')

angle = sheet['A1'].value

minute = sheet['B1'].value

seconds = sheet['C1'].value

calculations = angle + minute/60 + seconds/60

and also export this

`sys.stdout = open(input("name it :"), 'w')`

print(calculations)

But I cant figure out how to make that it could automaticaly count for next values, A2,B2,C2..... A7,B7,C7...... till all existing values. And after all, instead of 1 result, I could get all. E.g. If there were 10 calculations from A1,B1,C1........ A10,B10,C10 and get 10 calculations. If its easier it also can be a text data.

thank you for your time.

Answer

You can iterate over each row to do this. Just get number of rows, and simple for loop will be enough. For example:

```
book = openpyxl.load_workbook('excel.xlsx')
sheet = book.active
for row_i in range(2, sheet.max_row + 1):
deg_value = sheet.cell(row=row_i, column=1).value
min_value = sheet.cell(row=row_i, column=2).value
sec_value = sheet.cell(row=row_i, column=3).value
print(deg_value, min_value, sec_value)
```

Source (Stackoverflow)

Comments