简体   繁体   English

gspread:有没有办法将 dict 加载到 sheet.update_cells() 中?

[英]gspread: Is there a way to load a dict into sheet.update_cells()?

I have what I think is extremely sloppy code here:我有我认为非常草率的代码:

industry = []
headquarters = []
specialties = []
totalEmployeeCount = []
growth6Mth = []
website = []

for i in cvs_data:
    j = ci_data[0]
    for j in ci_data:
        if i['companyName'] == j['name']:
            industry.append(Cell(row = cvs_data.index(i)+2, col = 6,
                             value = j['industry']))
            headquarters.append(Cell(row = cvs_data.index(i)+2, col = 8,
                             value = j['headquarters']))
            specialties.append(Cell(row = cvs_data.index(i)+2, col = 9,
                             value = j['specialties']))
            totalEmployeeCount.append(Cell(row = cvs_data.index(i)+2, col = 10,
                             value = j['totalEmployeeCount']))
            growth6Mth.append(Cell(row = cvs_data.index(i)+2, col = 11,
                             value = j['growth6Mth']))
            website.append(Cell(row = cvs_data.index(i)+2, col = 14,
                             value = j['website']))
            
cvs.update_cells(industry)
cvs.update_cells(headquarters)
cvs.update_cells(specialties)
cvs.update_cells(totalEmployeeCount)
cvs.update_cells(growth6Mth)
cvs.update_cells(website)

Where cvs_data is a list of dicts that is used for its index.其中 cvs_data 是用于其索引的字典列表。 The actual gspread worksheet (cvs) will be updated with values from ci_data, another list of dicts.实际的 gspread 工作表 (cvs) 将使用来自 ci_data(另一个字典列表)的值进行更新。

I'm guessing my search could be better too.我猜我的搜索也可以更好。

Is it possible to append all those values (ie industry, headquarters, etc.) into one dictionary instead of individual lists, then call update_cells() on that one dictionary?是否可以将所有这些值(即行业、总部等)附加到一个字典而不是单个列表中,然后在该字典上调用 update_cells() ?

If your goal is to have one update_cells() , then you should be able to use:如果您的目标是拥有一个update_cells() ,那么您应该能够使用:

cvs.update_cells(industry + headquarters + specialties + ...)

Your Cell objects look like they all have row and column data, so they don't need to be in separate lists.您的 Cell 对象看起来都具有行和列数据,因此它们不需要位于单独的列表中。

I don't think I've seen anything that goes from a dict to a google sheet, but another way to do that (where the keys are industry, headquarters, etc. while the values are the list of Cells) would be:我认为我没有看到任何从 dict 到 google sheet 的内容,但是另一种方法(其中键是行业、总部等,而值是单元格列表)是:

all_data = {'industry': [], 'headquarters': [], ...}
# add cells to all_data
data = []
for list_of_cells in all_data.values():
    data.extend(list_of_cells)
cvs.update_cells(data)

For your searching, reorganizing ci_data from a list of dicts to one dict might help.对于您的搜索,将ci_data从字典列表重新组织为一个字典可能会有所帮助。 You can have the keys be the names while the values are the dicts:您可以将键作为名称,而将值作为字典:

ci_data_dict = dict()
for item in ci_data:
    name = item['name']
    ci_data_dict[name] = item

Then you won't have nesting for loops in your code:那么你的代码中就不会嵌套 for 循环:

for i in cvs_data:
    j = ci_data_dict[i['company_name']]
    industry.append(Cell(row = cvs_data.index(i) + 2, col = 6, value = j['industry']))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM