英文:
Appending no of lists to an existing excel file starting from a specific cells with Python, Openpyxl
问题
I'm getting no of list's in my_list
variable through iterating a dictionary -> employee_data
, it's not a list of list; it's coming through in a for loop
one by one. Look into the code below...
print(my_list)
['A', 'B', 'C', 'D', 'E']
['B', 'C', 'B', 'B', 'B']
['C', 'C', 'C', 'C', 'C']
['A', 'B', 'C', 'D', 'E']
['D', 'D', 'D', 'D', 'D']
- I just want to add those list's items in Excel from cell between
ws['C3':'H' + str(ws.max_row)]
like this. Is it possible in a loop?
And my code is:
for key, day_value in employee_data.items():
attendance_array = []
for val_key, get_value in day_value.items():
attendance_array.append(get_value)
my_list = attendance_array[n:]
print(my_list)
for rw in ws['C3':'H' + str(ws.max_row)]:
for cell, val in zip(rw, my_list):
cell.value = val
英文:
I'm getting no of list's in my_list
variable trough iterating a dictionary -> employee_data
, its not a list of list its coming trough in for loop
one by one, look into code below...
print(my_list)
['A', 'B', 'C', 'D', 'E']
['B', 'C', 'B', 'B', 'B']
['C', 'C', 'C', 'C', 'C']
['A', 'B', 'C', 'D', 'E']
['D', 'D', 'D', 'D', 'D']
- I just want to add those
list's
item inexcel
from cell betweenws['C3':'H' + str(ws.max_row)]
like this is it possible in loop ?
and my code is
for key,day_value in employee_data.items():
attendance_array = []
for val_key, get_value in day_value.items():
attendance_array.append(get_value)
my_list = attendance_array[n:]
print(my_list)
for rw in ws['C3':'H' + str(ws.max_row)]:
for cell, val in zip(rw, my_list):
cell.value = val
答案1
得分: 0
您的代码从employee_data字典中读取一个列表,预计是[ 'A', 'B', 'C', 'D', 'E'],然后将此列表应用于所有5行,因为您从C3到H7创建了一个范围,然后获得下一个列表,[ 'B', 'C', 'B', 'B', 'B'],然后执行相同的操作,以此类推处理其余的列表。不确定为什么范围需要扩展到H列,因为最后一列是G,但您需要在第一个循环中将范围设置为C3:G3,即仅处理第3行,然后在下一个循环中将范围设置为C4:G4,然后下一个循环设置为C5:G5等。
示例:
...
row = 3
for key, day_value in employee_data.items():
attendance_array = []
for val_key, get_value in day_value.items():
attendance_array.append(get_value)
my_list = attendance_array[n:]
print(my_list)
for rw in ws[f'C{row}:G{row}']:
for cell, val in zip(rw, my_list):
cell.value = val
row += 1
英文:
Your code reads 1 list from employee_data dictionary so presumably ['A', 'B', 'C', 'D', 'E'] then applies this one list to all 5 rows since your create a range from C3 to H7 then you get the next list, ['B', 'C', 'B', 'B', 'B'] and do the same thing and so on for the rest of the lists. Not sure why your range needs to extend to column H since the last column is G but you need to set the range the first loop as C3:G3 i.e. row 3 only then on next loop set range to C4:G4, then next loop C5:G5 etc<br>
Example<br>
...
row = 3
for key, day_value in employee_data.items():
attendance_array = []
for val_key, get_value in day_value.items():
attendance_array.append(get_value)
my_list = attendance_array[n:]
print(my_list)
for rw in ws[f'C{row}:G{row}']:
for cell, val in zip(rw, my_list):
cell.value = val
row += 1
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论