对于经验丰富的编码人员,我需要做的可能很简单。我的Python程序成功执行了以下操作:
我不仅需要打印这些值,还需要将它们添加到excel工作簿上的工作表中,并使用Model,Consumed,Avaible和Requestors作为列标题。
这是我打印值的方式:
if not REQUESTORLIST:
print(CURRENTMODEL, "Consumed:", CONSUMEDCOUNTER, "Available:", AVAILABLECOUNTER)
else:
print(CURRENTMODEL,"Consumed:",CONSUMEDCOUNTER, "Available:",AVAILABLECOUNTER,REQUESTORS)
以下是数据类型:
print(type(CURRENTMODEL))
print(type(CONSUMEDCOUNTER))
print(type(AVAILABLECOUNTER))
print(type(REQUESTORS))
输出:
<class 'str'>
<class 'int'>
<class 'int'>
<class 'collections.Counter'>
最后,程序输出被截断:
Model WS-SFP Consumed: 1 Available: 2 Counter({'Requester Anthony House': 1})
我是编程的新手(这是我编写的第一个程序),并且很难找到一种方法来获取这些值以用我需要的四个列标题写入excel工作表。我试图将它们转换为字符串并使用.write,但到目前为止仍未成功。你有什么建议吗?
编辑:感谢您的快速回复。我认为仅发布代码可能对我有所帮助。我很愿意就如何优化此问题提供反馈,因为它很可能低于标准。我一直在尝试.write,它会跳过excel工作表输出中的行,不填充列标题等。可能不是我的最佳选择。
import os
import openpyxl
import matplotlib
import numpy
import pandas as pd
import xlrd
import xlwt
from xlwt import Workbook
import xlsxwriter
from collections import Counter
#file to pull data from
excel_file = 'Customer_Inventory_Test.xlsx'
models = pd.read_excel(excel_file)
#file to export results
workbook = xlsxwriter.Workbook('Inventory Report.xlsx')
worksheet = workbook.add_worksheet()
row = 0
col = 0
ROWCOUNT = models.shape[0]
while True:
CONSUMEDCOUNTER = 0
AVAILABLECOUNTER = 0
REQUESTORLIST = []
#break when no more rows
if row == ROWCOUNT:
break
MODEL = models.iloc[row, [0]]
#convert to string for comparison
MODEL = MODEL.to_string()
CURRENTMODEL = MODEL
LOCATION = models.iloc[row, [2]]
LOCATION = LOCATION.to_string()
while CURRENTMODEL == MODEL:
if "Consumed" in LOCATION:
CONSUMEDCOUNTER += 1
REQUESTOR = models.iloc[row, [17]]
# convert to string for comparison
REQUESTOR = REQUESTOR.to_string()
REQUESTORLIST.append(REQUESTOR)
else:
AVAILABLECOUNTER += 1
row += 1
if row == ROWCOUNT:
break
MODEL = models.iloc[row, [0]]
MODEL = MODEL.to_string()
LOCATION = models.iloc[row, [2]]
LOCATION = LOCATION.to_string()
REQUESTORS = Counter(REQUESTORLIST)
if not REQUESTORLIST:
worksheet.write(row, col, CURRENTMODEL)
worksheet.write(row, col + 1, CONSUMEDCOUNTER)
worksheet.write(row, col + 2, AVAILABLECOUNTER)
print(CURRENTMODEL[9:], "Consumed:", CONSUMEDCOUNTER, "Available:",
AVAILABLECOUNTER)
else:
worksheet.write(row, col, CURRENTMODEL)
worksheet.write(row, col + 1, CONSUMEDCOUNTER)
worksheet.write(row, col + 2, AVAILABLECOUNTER)
#worksheet.write(row, col + 3, REQUESTORS) <- Doesn't like
#requestors data structure
print(CURRENTMODEL[9:],"Consumed:",CONSUMEDCOUNTER,
"Available:",AVAILABLECOUNTER,REQUESTORS)
workbook.close()
答案 0 :(得分:0)
欢迎使用堆栈溢出。签出此DataFrame方法:.to_excel()。您需要从数据框中调用此方法,并提供一个文件路径,该路径将是新Excel文件所在的位置。例如:my_data_frame.to_excel('path/to/my/new_file.xlsx')
。
答案 1 :(得分:0)
您可以提供代码的更多详细信息,因此我们可以更轻松地为您提供帮助。无论如何,有一些方法可以满足您的需求,但是我建议您使用熊猫库。
以下是一些示例链接。
https://www.datacamp.com/community/tutorials/pandas-tutorial-dataframe-python https://xlsxwriter.readthedocs.io/working_with_pandas.html
PS:您甚至可以使用大熊猫的结构进行计数。
答案 2 :(得分:0)
我做了更多的尝试,将列表转换回一个数据帧,看来我现在有一个可行的解决方案。
这就是我所拥有的:
#NEWLIST is the list that changes with each loop iteration.
NEWLIST = [(CURRENTMODEL[9:], CONSUMEDCOUNTER, AVAILABLECOUNTER, REQUESTORS)]
CURRENTLIST.extend(NEWLIST)
df = pd.DataFrame(CURRENTLIST, columns=["Model","Consumed","Available","Requestor(s)"])
writer = pd.ExcelWriter('Inventory Summary Report.xlsx')
df.to_excel(writer,'Sheet1',freeze_panes=(1,1), index=False)