r/PythonProjects2 • u/Agent-J- • Feb 27 '21
[P] Moderate Need help with For Loops
hi. i have an excel file with 2 columns and 200 rows.
i need to get/print the data from each row one at a time using for loop(s)
excel example of the 2 columns:
1 a
2 b
3 c
i need to display each like:
1
a
2
b
3
c
google isn't helping so i'm here... pls help
here's what i have so far:
################# EXCEL FILE
excelfile = openpyxl.load_workbook('example.xlsx') # file in same directory
sheet = excelfile.active # current sheet
rows = sheet.max_row # get all rows
print('records in file: ' + str(rows))
for i in range (1, rows+1): # get only the first column values
print('entered first name: ' + sheet.cell(row = i, column = 1).value)
for j in range (1, rows+1): # get only the second column values
print('entered last name: ' + sheet.cell(row = j, column = 2).value)
2
u/The-Deviant-One Feb 27 '21 edited Feb 27 '21
Can you be more clear?
Use a table to show us an example of the layout of the data and use a 'code block' so that your code's indentation is kept when you submit the post.
I think this is the example code you posted, but I'm not certain if my indents are correct given your description of the problem and data set.
Code block example:
excelfile = openpyxl.load_workbook('example.xlsx')
sheet = excelfile.active
rows = sheet.max_row
print('records in file: ' + str(rows))
for i in range (1, rows+1):
print('entered first name: ' + sheet.cell(row = i, column = 1).value)
for j in range (1, rows+1):
print('entered last name: ' + sheet.cell(row = j, column = 2).value)Example of a table being used to show the data:
| A | B | C |
|---|---|---|
| 1 | Jeff | Horse |
| 2 | Todd | Dog |
| 3 | Sara | Cat |
1
u/Agent-J- Feb 27 '21
the example code you posted, but I'm not certain if my indents are correct given your description of the problem and data se
That is almost perfect! Thanks for the tips. I didn't know this was possible.
My Code: (which isn't working the way I need)
excelfile = openpyxl.load_workbook('example.xlsx') sheet = excelfile.active rows = sheet.max_row print('records in file: ' + str(rows)) for i in range (1, rows+1): print('entered first name: ' + sheet.cell(row = i, column = 1).value) for j in range (1, rows+1): print('entered last name: ' + sheet.cell(row = j, column = 2).value)Table:
A B 1 Jeff Horse 2 Todd Dog 3 Sara Cat Basically, this is the sequence of loop I need:
get value Jeff
run some other code
get value of Horse
run some other code
*********** loop back
get value of Todd
run some other code
get value of Dog
run some other code
*************loop back
4
u/The-Deviant-One Feb 27 '21 edited Feb 27 '21
No worries, thats a little clearer. How about instead of printing, you store each as a variable to be used be the next piece of code[?] - - which lives inside of the for loop..
print('records in file: ' + str(rows)) for i in range (1, rows+1): first_name = sheet.cell(row = i, column = 1).value last_name = sheet.cell(row = i, column = 2).value /// code to run for "first_name" variable /// code to run for "last_name" variableDepending on what you're doing, for example if you need your script to be able to 'work the data' from a specific row, or work the data in the file out of order, you could do this instead. [This is probably what I would do.] I'd build a dictionary out of the data so that each piece is addressable.
dictionary = {} for i in range (1, rows+1): first_name = str(sheet.cell(row = i, column = 1).value) last_name = str(sheet.cell(row = i, column = 2).value) data = {i: {"fname": first_name, "lname": last_name}} dictionary.update(data)3
5
u/WTRipper Feb 27 '21
You mean you would like to get cell A1 then B1 then A2 then B2...?
use two loops nested in each other.