简体   繁体   English

如何在Python中将字典输出到Excel工作表

[英]How to output my dictionary to an Excel sheet in Python

Background: My first Excel-related script. 背景:我的第一个与Excel相关的脚本。 Using openpyxl. 使用openpyxl。 There is an Excel sheet with loads of different types of data about products in different columns. 有一个Excel工作表,其中包含有关不同列中产品的不同类型数据的负载。

My goal is to extract certain types of data from certain columns (eg price, barcode, status), assign those to the unique product code and then output product code, price, barcode and status to a new excel doc. 我的目标是从某些列中提取某些类型的数据(例如价格,条形码,状态),将这些数据分配给唯一的产品代码,然后将产品代码,价格,条形码和状态输出到新的excel文档。

I have succeeded in extracting the data and putting it the following dictionary format: 我已成功提取数据并将其放入以下字典格式:

productData = {'AB123': {'barcode': 123456, 'price': 50, 'status': 'NEW'}

My general thinking on getting this output to a new report is something like this (although I know that this is wrong): 我对将输出结果输出到新报告中的一般想法是这样的(尽管我知道这是错误的):

newReport = openpyxl.Workbook()
newSheet = newReport.active
newSheet.title = 'Output'

newSheet['A1'].value = 'Product Code'
newSheet['B1'].value = 'Price'
newSheet['C1'].value = 'Barcode'
newSheet['D1'].value = 'Status'

for row in range(2, len(productData) + 1):
    newSheet['A' + str(row)].value = productData[productCode]
    newSheet['B' + str(row)].value = productPrice
    newSheet['C' + str(row)].value = productBarcode
    newSheet['D' + str(row)].value = productStatus

newReport.save('ihopethisworks.xlsx')

What do I actually need to do to output the data? 我实际上需要做什么来输出数据?

I would suggest using Pandas for that. 我建议为此使用熊猫。 It has the following syntax: 它具有以下语法:

df = pd.read_excel('your_file.xlsx')
df['Column name you want'].to_excel('new_file.xlsx')

You can do a lot more with it. 您可以用它做更多的事情。 Openpyxl might not be the right tool for your task (Openpyxl is too general). Openpyxl可能不是适合您的任务的工具(Openpyxl太笼统了)。

PS I would leave this in the comments, but stackoverflow, in their widom decided to let anyone to leave answers, but not to comment. PS:我将在评论中保留此内容,但出于他们的智慧,stackoverflow决定让任何人留下答案,但不发表评论。

The logic you use to extract the data is missing but I suspect the best approach is to use it to loop over the two worksheets in parallel. 您提取数据所使用的逻辑丢失了,但是我怀疑最好的方法是使用它来并行遍历两个工作表。 You can then avoid using a dictionary entirely and just append loops to the new worksheet. 然后,您可以避免完全使用字典,而只需将循环附加到新工作表即可。

Pseudocode: 伪代码:

ws1 # source worksheet
ws2 # new worksheet

product = []
code = ws1[…] # some lookup
barcode = ws1[…]
price = ws1[…]
status = ws1[…]

ws2.append([code, price, barcode, status])

Pandas will work best for this here are some examples 熊猫在这方面最有效

import pandas as pd

#df columns:  Date    Open    High     Low   Close     Volume
#reading data from an excel 
df = pd.read_excel('GOOG-NYSE_SPY.xls')

#set index to the column of your choice, in this case it would be date
df.set_index('Date', inplace = True)

#choosing the columns of your choice for further manipulation
df = df[['Open', 'Close']]

#divide two colums to get the % change
df = (df['Open'] - df['Close']) / df['Close'] * 100


print(df.head())

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

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