gpt4 book ai didi

openpyxl AttributeError when i attempt to add data in dict from multiple sheets(当我尝试从多个工作表添加词典中的数据时,Openpyxl AttributeError)

转载 作者:bug小助手 更新时间:2023-10-25 13:39:48 32 4
gpt4 key购买 nike



I want to add the data from Excel into a dictionary. But an error appears when I use .append(total_sales) and of course no problem if I do +=total_sales, except that I obtain the sum instead of having the data for the 3 separate months. Can you help me find the error in my code?

我想将Excel中的数据添加到词典中。但是,当我使用.append(TOTAL_SALES)时出现错误,当然,如果我使用+=TOTAL_SALES,则没有问题,只是我获得的是总和,而不是3个单独月份的数据。你能帮我找出代码中的错误吗?


import openpyxl
#juste recuperer les valeurs pas les formules "data_only=True"
wb1 = openpyxl.load_workbook("octobre.xlsx", data_only=True)
wb2 = openpyxl.load_workbook("novembre.xlsx", data_only=True)
wb3 = openpyxl.load_workbook("decembre.xlsx", data_only=True)


def add_data_from_wb(wb, data):
sheet = wb.active
for row in range(2, sheet.max_row):
item = sheet.cell(row, 1).value
if not item:
break
total_sales = sheet.cell(row, 4).value
if data.get(item):
data[item].append(total_sales)
else:
data[item] = total_sales

data_dict = {}
add_data_from_wb(wb1, data_dict)
add_data_from_wb(wb2, data_dict)
add_data_from_wb(wb3, data_dict)
print(data_dict)


更多回答

Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.

请澄清您的具体问题或提供更多详细信息,以突出您的确切需求。按照目前的写法,很难准确地说出你在问什么。

优秀答案推荐

I'm trying to understand (from your code) the exact format of your spreadsheets and the final dictionary you're expecting as an output. I imagine that you expect a dictionary with items/strings as keys and lists of integers/total_sales as the values.

我正在尝试(从您的代码)了解您的电子表格的确切格式以及您期望作为输出的最终词典。我想您会期望得到一个以项/字符串为键、以整数列表/TOTAL_SALES为值的字典。


I can reproduce your AttributeError: 'int' object has no attribute 'append' and that's because you're initiating a dictionnary with integers rather than a list of integers. This can be fixed by using data[item] = [total_sales] (line 18 of your code).

我可以重现您的AttributeError:‘int’对象没有‘append’属性,这是因为您正在使用整数而不是整数列表启动字典。这可以通过使用data[Item]=[TOTAL_SALES](代码的第18行)来修复。


You can also simply your code by making a list of files and a defaultdict :

您还可以通过创建文件列表和默认字典来简化您的代码:


from openpyxl import load_workbook
from collections import defaultdict

l = ["octobre.xlsx", "novembre.xlsx", "decembre.xlsx"]

def add_data_from_wbs(files):
data = defaultdict(list)
for f in files:
sheet = load_workbook(f, data_only=True).active
for row in range(2, sheet.max_row+1):
item = sheet.cell(row, 1).value
total_sales = sheet.cell(row, 4).value
data[item].append(total_sales)
return dict(data)

d = add_data_from_wbs(l)

A variant, using GroupBy.agg / to_dict from :

一个变体,使用来自熊猫的GroupBy.agg/to_dict:


#pip install pandas
import pandas as pd

d = (
pd.concat([pd.read_excel(f, header=None, skiprows=1) for f in l])
.groupby(0)[3].agg(list).to_dict()
)

Output :

输出:


print(d) #{'Item1': [100, 300], 'Item2': [200], 'Item3': [300, 500, 400]}

Used inputs :

使用的输入:


enter image description here


更多回答

That's exactly what I wanted to do. I'll try to be more precise next time :) Thank you for taking the time to try to understand and answer.

这正是我想要做的。下一次我会尽量说得更准确:)感谢您抽出时间来理解和回答。

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com