因为我的一个供应商,我遇到了一个问题。出于某种原因,每当我通过他们的统计API运行任何报告时,它总是使用太平洋标准时间运行的,而不管我是在东部标准时间。为了解决这个问题,我必须运行报告,将开始和结束日期往回拨三个小时,然后我需要手动将"TimeStamp“列的时间向前拨三个小时。最后,我需要所有的结果输入到我的MS SQL实例。我已经到了可以拿回结果的地步,但我被困在了下一步该做什么上。我的直觉告诉我这可能是一个熊猫解决方案,但我不确定如何将结果输入到熊猫数据框架中。下面是我目前所拥有的(请注意,我正在与之合作的供应商名为Five9,我为他们找到了一个库,它可以帮助我连接到API并获得我想要的报告结果):
from five9 import Five9
import datetime
from datetime import datetime, timedelta
import time
from pytz import timezone
import pyodbc
import json
now_utc = datetime.now(timezone('UTC'))
now_eastern = now_utc.astimezone(timezone('US/Eastern'))
#Change days from current time
startreportime = now_eastern - timedelta(days=2)
endreportime = now_eastern - timedelta(days=1)
#Set start and end time for report criteria
starttime = f"{(startreportime):%Y-%m-%d}" + 'T21:00:00.000'
endtime = f"{(endreportime):%Y-%m-%d}" + 'T20:59:00.000'
#connect to API
client = Five9('MyUID','MyPWD')
#Set variables as start and end
start = starttime
end = endtime
#set criteria using variables
criteria = {'time':{'end':end, 'start':start}}
#Get report and seet criteria for report
identifier = client.configuration.runReport(folderName='Five9 Import Data',\
reportName='Agent State Details',criteria=criteria)
#Sleep so report has time to complete
time.sleep(30)
#Get report results
get_results = client.configuration.getReportResult(identifier)
results = get_results['records']
print(results)使用它,我得到了以下类型的结果:
[{
'values': {
'data': [
'Mon, 22 Feb 2021 21:00:00',
'abowling@*****.com',
'Adam',
'Bowling',
'Login',
None,
None,
'TUPSS, Telamon Inbound, Stericycle Environment Inbound, Stericycle ComSol Inbound,
'01:18:05',
'08 - TS'
]
} 如果我可以将这些结果放入数据帧中,我非常确定我可以管理其余的内容。我知道如何使用时间增量来处理时间戳问题,并且我可以处理从dataframe到sql的转换。我正在努力弄清楚如何将这些结果放入数据框中。
发布于 2021-02-26 23:06:45
不确定是否有人会读这篇文章,但我得到了以下内容:
def process_rows(rows):
for row in rows:
date1 = row['values']['data'][0]
date1 = datetime.strptime(date1, '%a, %d %b %Y %H:%M:%S').astimezone(timezone("US/Pacific"))
date2 = date1.astimezone(timezone("US/Eastern"))
date2 = date2.strftime('%Y-%m-%d %H:%M:%S')
cloned_row = [value for value in row['values']['data']]
cloned_row[0] = str(date2)
if cloned_row[8] == '24:00:00':
cloned_row[8] = '00:00:00'
yield cloned_row
args = process_rows(results)
insertSQL = ('''
INSERT INTO [Reporting].[dbo].[AgentState]
(TimeStamp, Agent, FirstName, LastName, State, ReasonCode, Media, Skill, StateTime, [Group])
VALUES (?,?,?,?,?,?,?,?,?,?)
'''
)
cursor.fast_executemany = True
cursor.executemany(insertSQL, args)
conn.commit()https://stackoverflow.com/questions/66354556
复制相似问题