我正在使用此class
在xlsx
个文件中编写统计信息:
class DictWriter:
def __init__(self, workbook, worksheet, fieldnames):
self.workbook = workbook
self.worksheet = worksheet
self.fieldnames = fieldnames
self.nrow = 1
def writerow(self, d, condition=0):
for k in d:
ncol = self.fieldnames.index(k)
self.worksheet.write(self.nrow, ncol, d[k])
self.nrow += 1
def writeheader(self):
for ncol, fieldname in enumerate(self.fieldnames):
self.worksheet.write(0, ncol, fieldname)
以这种方式使用它:
def file_generator():
link = "http://linkforexample.com/api/getSomething"
client = requests.get(link)
response = client.json()['response']
if response['httpStatus'] == 200:
workbook = xlsxwriter.Workbook('Statistics.xlsx', {'strings_to_numbers': True})
worksheet = workbook.add_worksheet()
data_table = response['data']['data']
fieldnames = ["Date", "Advertiser", "Clicks", "Conversions", "Cost", "Revenue", "Profit"]
writer = DictWriter(workbook, worksheet, fieldnames)
writer.writeheader()
for d in data_table:
warning = (float(real_profit)/float(d['Stat']['revenue']))*100
row_dict = {"Date": d['Stat']['date'], "Advertiser": d['Advertiser']['company'], "Clicks": d['Stat']['clicks'], "Conversions": d['Stat']['conversions'], "Cost": d['Stat']['payout'], "Revenue": d['Stat']['revenue'], "Profit": d['Stat']['profit']}
writer.writerow(row_dict)
如何创建一个带有n-1值的column
,这些值将包含行X中当前单元格中的值与前一个值之间的差异?
谢谢!