我有一个脚本,它在工时记录卡系统中进行API调用并接收JSON数据,包括员工和他们的计时卡。最终目标是将这些数据解析成我需要的内容,然后将这些数据插入到sql表中。问题是,到目前为止,我只为一个员工生成了所需的输出,而不是所有员工。
我尝试过循环,并尝试创建新的字典等,但对于我想要实现的目标,每个循环都有各自的缺点。
下面是我试图向下解析的JSON输出:
[
{
"BreakApprovalStatusId": null,
"BreakApprovalStatusName": null,
"BreakMinutes": 0,
"DoubleTimeHours": 0.0,
"Duration": "07:00:00",
"Employee": {
"Email": "*************************",
"EmployeeId": null,
"FirstName": "Nicholas",
"FullName": "Nicholas ******",
"Id": 346186,
"IsActive": true,
"LastName": "******",
"Username": "*******"
},
"Hours": 7.0,
"Id": 13026137,
"JobCodeId": null,
"JobCodeName": "",
"LocationId": null,
"LocationName": "",
"OverTimeHours": 0.0,
"PTOEarningCodeAbbr": "",
"PTOEarningCodeId": null,
"PTOHours": null,
"PunchInApprovalStatusId": 4,
"PunchInApprovalStatusName": "Changed By Manager",
"PunchInDateTime": "2019-06-19T07:49:00",
"PunchInImageUrl": "",
"PunchInIpAddress": "*****",
"PunchInLatitude": null,
"PunchInLongitude": null,
"PunchInNotes": "",
"PunchOutApprovalStatusId": 4,
"PunchOutApprovalStatusName": "Changed By Manager",
"PunchOutDateTime": "2019-06-19T14:49:00",
"PunchOutImageUrl": "",
"PunchOutIpAddress": "****",
"PunchOutLatitude": null,
"PunchOutLongitude": null,
"PunchOutNotes": "",
"RegularHours": 7.0
}
]
[
{
"BreakApprovalStatusId": null,
"BreakApprovalStatusName": null,
"BreakMinutes": 0,
"DoubleTimeHours": 0.0,
"Duration": "05:00:00",
"Employee": {
"Email": "********",
"EmployeeId": null,
"FirstName": "Test",
"FullName": "Test MBS",
"Id": 346188,
"IsActive": true,
"LastName": "MBS",
"Username": "testmbs"
},
"Hours": 5.0,
"Id": 13026141,
"JobCodeId": null,
"JobCodeName": "",
"LocationId": null,
"LocationName": "",
"OverTimeHours": 0.0,
"PTOEarningCodeAbbr": "",
"PTOEarningCodeId": null,
"PTOHours": null,
"PunchInApprovalStatusId": 4,
"PunchInApprovalStatusName": "Changed By Manager",
"PunchInDateTime": "2019-06-19T09:49:00",
"PunchInImageUrl": "",
"PunchInIpAddress": "****",
"PunchInLatitude": null,
"PunchInLongitude": null,
"PunchInNotes": "",
"PunchOutApprovalStatusId": 4,
"PunchOutApprovalStatusName": "Changed By Manager",
"PunchOutDateTime": "2019-06-19T14:49:00",
"PunchOutImageUrl": "",
"PunchOutIpAddress": "****",
"PunchOutLatitude": null,
"PunchOutLongitude": null,
"PunchOutNotes": "",
"RegularHours": 5.0
}
]
[
{
"BreakApprovalStatusId": null,
"BreakApprovalStatusName": null,
"BreakMinutes": 0,
"DoubleTimeHours": 0.0,
"Duration": "05:00:00",
"Employee": {
"Email": "starlord@email.com",
"EmployeeId": "080097",
"FirstName": "Peter",
"FullName": "Peter Quill",
"Id": 346968,
"IsActive": true,
"LastName": "Quill",
"Username": "starlord"
},
"Hours": 5.0,
"Id": 12970292,
"JobCodeId": null,
"JobCodeName": "",
"LocationId": null,
"LocationName": "",
"OverTimeHours": 0.0,
"PTOEarningCodeAbbr": "",
"PTOEarningCodeId": null,
"PTOHours": null,
"PunchInApprovalStatusId": 4,
"PunchInApprovalStatusName": "Changed By Manager",
"PunchInDateTime": "2019-06-18T08:43:00",
"PunchInImageUrl": "",
"PunchInIpAddress": "****",
"PunchInLatitude": null,
"PunchInLongitude": null,
"PunchInNotes": "",
"PunchOutApprovalStatusId": 4,
"PunchOutApprovalStatusName": "Changed By Manager",
"PunchOutDateTime": "2019-06-18T13:43:00",
"PunchOutImageUrl": "",
"PunchOutIpAddress": "****",
"PunchOutLatitude": null,
"PunchOutLongitude": null,
"PunchOutNotes": "",
"RegularHours": 5.0
},
{
"BreakApprovalStatusId": null,
"BreakApprovalStatusName": null,
"BreakMinutes": 0,
"DoubleTimeHours": 0.0,
"Duration": "01:16:00",
"Employee": {
"Email": "starlord@email.com",
"EmployeeId": "080097",
"FirstName": "Peter",
"FullName": "Peter Quill",
"Id": 346968,
"IsActive": true,
"LastName": "Quill",
"Username": "starlord"
},
"Hours": 1.267,
"Id": 12983841,
"JobCodeId": null,
"JobCodeName": "",
"LocationId": null,
"LocationName": "",
"OverTimeHours": 0.0,
"PTOEarningCodeAbbr": "",
"PTOEarningCodeId": null,
"PTOHours": null,
"PunchInApprovalStatusId": 4,
"PunchInApprovalStatusName": "Changed By Manager",
"PunchInDateTime": "2019-06-19T08:00:00",
"PunchInImageUrl": "",
"PunchInIpAddress": "****",
"PunchInLatitude": null,
"PunchInLongitude": null,
"PunchInNotes": "",
"PunchOutApprovalStatusId": 4,
"PunchOutApprovalStatusName": "Changed By Manager",
"PunchOutDateTime": "2019-06-19T09:16:00",
"PunchOutImageUrl": "",
"PunchOutIpAddress": "****",
"PunchOutLatitude": null,
"PunchOutLongitude": null,
"PunchOutNotes": "",
"RegularHours": 1.267
},
{
"BreakApprovalStatusId": null,
"BreakApprovalStatusName": null,
"BreakMinutes": 0,
"DoubleTimeHours": 0.0,
"Duration": "03:00:00",
"Employee": {
"Email": "starlord@email.com",
"EmployeeId": "080097",
"FirstName": "Peter",
"FullName": "Peter Quill",
"Id": 346968,
"IsActive": true,
"LastName": "Quill",
"Username": "starlord"
},
"Hours": 3.0,
"Id": 13007880,
"JobCodeId": null,
"JobCodeName": "",
"LocationId": null,
"LocationName": "",
"OverTimeHours": 0.0,
"PTOEarningCodeAbbr": "",
"PTOEarningCodeId": null,
"PTOHours": null,
"PunchInApprovalStatusId": 4,
"PunchInApprovalStatusName": "Changed By Manager",
"PunchInDateTime": "2019-06-19T13:43:00",
"PunchInImageUrl": "",
"PunchInIpAddress": "****",
"PunchInLatitude": null,
"PunchInLongitude": null,
"PunchInNotes": "",
"PunchOutApprovalStatusId": 4,
"PunchOutApprovalStatusName": "Changed By Manager",
"PunchOutDateTime": "2019-06-19T16:43:00",
"PunchOutImageUrl": "",
"PunchOutIpAddress": "****",
"PunchOutLatitude": null,
"PunchOutLongitude": null,
"PunchOutNotes": "",
"RegularHours": 3.0
},
{
"BreakApprovalStatusId": null,
"BreakApprovalStatusName": null,
"BreakMinutes": 0,
"DoubleTimeHours": 0.0,
"Duration": "04:00:00",
"Employee": {
"Email": "starlord@email.com",
"EmployeeId": "080097",
"FirstName": "Peter",
"FullName": "Peter Quill",
"Id": 346968,
"IsActive": true,
"LastName": "Quill",
"Username": "starlord"
},
"Hours": 4.0,
"Id": 13022834,
"JobCodeId": null,
"JobCodeName": "",
"LocationId": null,
"LocationName": "",
"OverTimeHours": 0.267,
"PTOEarningCodeAbbr": "",
"PTOEarningCodeId": null,
"PTOHours": null,
"PunchInApprovalStatusId": 4,
"PunchInApprovalStatusName": "Changed By Manager",
"PunchInDateTime": "2019-06-19T17:21:00",
"PunchInImageUrl": "",
"PunchInIpAddress": "****",
"PunchInLatitude": null,
"PunchInLongitude": null,
"PunchInNotes": "",
"PunchOutApprovalStatusId": 4,
"PunchOutApprovalStatusName": "Changed By Manager",
"PunchOutDateTime": "2019-06-19T21:21:00",
"PunchOutImageUrl": "",
"PunchOutIpAddress": "****",
"PunchOutLatitude": null,
"PunchOutLongitude": null,
"PunchOutNotes": "",
"RegularHours": 3.733
}
]
[
{
"BreakApprovalStatusId": null,
"BreakApprovalStatusName": null,
"BreakMinutes": 0,
"DoubleTimeHours": 0.0,
"Duration": "15:33:00",
"Employee": {
"Email": "****",
"EmployeeId": "080098",
"FirstName": "Kelly",
"FullName": "Kelly ****",
"Id": 346187,
"IsActive": true,
"LastName": "****",
"Username": "****"
},
"Hours": 15.55,
"Id": 12963731,
"JobCodeId": 17673,
"JobCodeName": "Test dept 1",
"LocationId": null,
"LocationName": "",
"OverTimeHours": 0.0,
"PTOEarningCodeAbbr": "",
"PTOEarningCodeId": null,
"PTOHours": null,
"PunchInApprovalStatusId": 4,
"PunchInApprovalStatusName": "Changed By Manager",
"PunchInDateTime": "2019-06-18T08:27:00",
"PunchInImageUrl": "",
"PunchInIpAddress": "****",
"PunchInLatitude": null,
"PunchInLongitude": null,
"PunchInNotes": "",
"PunchOutApprovalStatusId": 2,
"PunchOutApprovalStatusName": "Approved",
"PunchOutDateTime": "2019-06-19T00:00:00",
"PunchOutImageUrl": "",
"PunchOutIpAddress": "****",
"PunchOutLatitude": null,
"PunchOutLongitude": null,
"PunchOutNotes": "",
"RegularHours": 15.55
},
{
"BreakApprovalStatusId": null,
"BreakApprovalStatusName": null,
"BreakMinutes": 0,
"DoubleTimeHours": 0.0,
"Duration": "04:27:00",
"Employee": {
"Email": "****",
"EmployeeId": "080098",
"FirstName": "Kelly",
"FullName": "Kelly ****",
"Id": 346187,
"IsActive": true,
"LastName": "****",
"Username": "****"
},
"Hours": 4.45,
"Id": 12974763,
"JobCodeId": 17673,
"JobCodeName": "Test dept 1",
"LocationId": null,
"LocationName": "",
"OverTimeHours": 0.0,
"PTOEarningCodeAbbr": "",
"PTOEarningCodeId": null,
"PTOHours": null,
"PunchInApprovalStatusId": 4,
"PunchInApprovalStatusName": "Changed By Manager",
"PunchInDateTime": "2019-06-19T00:00:00",
"PunchInImageUrl": "",
"PunchInIpAddress": "****",
"PunchInLatitude": null,
"PunchInLongitude": null,
"PunchInNotes": "Shift automatically closed after 20 hours.",
"PunchOutApprovalStatusId": 2,
"PunchOutApprovalStatusName": "Approved",
"PunchOutDateTime": "2019-06-19T04:27:06.677",
"PunchOutImageUrl": "",
"PunchOutIpAddress": "****",
"PunchOutLatitude": null,
"PunchOutLongitude": null,
"PunchOutNotes": "Shift automatically closed after 20 hours.",
"RegularHours": 4.45
},
{
"BreakApprovalStatusId": null,
"BreakApprovalStatusName": null,
"BreakMinutes": 0,
"DoubleTimeHours": 0.0,
"Duration": "05:00:00",
"Employee": {
"Email": "kellyw@marinercomputer.com",
"EmployeeId": "080098",
"FirstName": "Kelly",
"FullName": "Kelly ****",
"Id": 346187,
"IsActive": true,
"LastName": "****",
"Username": "****"
},
"Hours": 5.0,
"Id": 12980029,
"JobCodeId": null,
"JobCodeName": "",
"LocationId": null,
"LocationName": "",
"OverTimeHours": 0.0,
"PTOEarningCodeAbbr": "",
"PTOEarningCodeId": null,
"PTOHours": null,
"PunchInApprovalStatusId": 5,
"PunchInApprovalStatusName": "Time Recorded",
"PunchInDateTime": "2019-06-19T07:13:32.12",
"PunchInImageUrl": "",
"PunchInIpAddress": "****",
"PunchInLatitude": null,
"PunchInLongitude": null,
"PunchInNotes": "",
"PunchOutApprovalStatusId": 4,
"PunchOutApprovalStatusName": "Changed By Manager",
"PunchOutDateTime": "2019-06-19T12:13:00",
"PunchOutImageUrl": "",
"PunchOutIpAddress": "****",
"PunchOutLatitude": null,
"PunchOutLongitude": null,
"PunchOutNotes": "",
"RegularHours": 5.0
},
{
"BreakApprovalStatusId": null,
"BreakApprovalStatusName": null,
"BreakMinutes": 0,
"DoubleTimeHours": 0.0,
"Duration": "10:00:00",
"Employee": {
"Email": "****",
"EmployeeId": "080098",
"FirstName": "Kelly",
"FullName": "Kelly ****",
"Id": 346187,
"IsActive": true,
"LastName": "****",
"Username": "****"
},
"Hours": 10.0,
"Id": 13064117,
"JobCodeId": null,
"JobCodeName": "",
"LocationId": null,
"LocationName": "",
"OverTimeHours": 0.0,
"PTOEarningCodeAbbr": "",
"PTOEarningCodeId": null,
"PTOHours": null,
"PunchInApprovalStatusId": 5,
"PunchInApprovalStatusName": "Time Recorded",
"PunchInDateTime": "2019-06-25T08:15:22.057",
"PunchInImageUrl": "",
"PunchInIpAddress": "****",
"PunchInLatitude": null,
"PunchInLongitude": null,
"PunchInNotes": "",
"PunchOutApprovalStatusId": 2,
"PunchOutApprovalStatusName": "Approved",
"PunchOutDateTime": "2019-06-25T18:15:00",
"PunchOutImageUrl": "",
"PunchOutIpAddress": "****",
"PunchOutLatitude": null,
"PunchOutLongitude": null,
"PunchOutNotes": "Shift automatically closed after 10 hours.",
"RegularHours": 10.0
},
{
"BreakApprovalStatusId": null,
"BreakApprovalStatusName": null,
"BreakMinutes": 0,
"DoubleTimeHours": 0.0,
"Duration": null,
"Employee": {
"Email": "****",
"EmployeeId": "080098",
"FirstName": "Kelly",
"FullName": "Kelly ****",
"Id": 346187,
"IsActive": true,
"LastName": "****",
"Username": "****"
},
"Hours": null,
"Id": 13079310,
"JobCodeId": null,
"JobCodeName": "",
"LocationId": null,
"LocationName": "",
"OverTimeHours": 0.0,
"PTOEarningCodeAbbr": "",
"PTOEarningCodeId": null,
"PTOHours": null,
"PunchInApprovalStatusId": 5,
"PunchInApprovalStatusName": "Time Recorded",
"PunchInDateTime": "2019-06-26T06:32:03.1",
"PunchInImageUrl": "",
"PunchInIpAddress": "****",
"PunchInLatitude": null,
"PunchInLongitude": null,
"PunchInNotes": "",
"PunchOutApprovalStatusId": null,
"PunchOutApprovalStatusName": null,
"PunchOutDateTime": null,
"PunchOutImageUrl": "",
"PunchOutIpAddress": null,
"PunchOutLatitude": null,
"PunchOutLongitude": null,
"PunchOutNotes": "",
"RegularHours": 0.0
},
{
"BreakApprovalStatusId": null,
"BreakApprovalStatusName": null,
"BreakMinutes": 0,
"DoubleTimeHours": 0.0,
"Duration": "09:30:00",
"Employee": {
"Email": "****",
"EmployeeId": "080098",
"FirstName": "Kelly",
"FullName": "Kelly ****",
"Id": 346187,
"IsActive": true,
"LastName": "****",
"Username": "****"
},
"Hours": 9.5,
"Id": 13084304,
"JobCodeId": null,
"JobCodeName": "",
"LocationId": null,
"LocationName": "",
"OverTimeHours": 0.0,
"PTOEarningCodeAbbr": "",
"PTOEarningCodeId": null,
"PTOHours": null,
"PunchInApprovalStatusId": 4,
"PunchInApprovalStatusName": "Changed By Manager",
"PunchInDateTime": "2019-06-21T06:30:00",
"PunchInImageUrl": "",
"PunchInIpAddress": "****",
"PunchInLatitude": null,
"PunchInLongitude": null,
"PunchInNotes": "",
"PunchOutApprovalStatusId": 4,
"PunchOutApprovalStatusName": "Changed By Manager",
"PunchOutDateTime": "2019-06-21T16:00:00",
"PunchOutImageUrl": "",
"PunchOutIpAddress": "****",
"PunchOutLatitude": null,
"PunchOutLongitude": null,
"PunchOutNotes": "",
"RegularHours": 9.5
}
]正如你所看到的,这是大量的数据,这只是7天内的4名员工,这也可能更长。这段json代码由以下代码输出:
conn = httplib.HTTPSConnection('****.azure-api.net')
conn.request("GET", "/timecard/GetByPayPeriod/525117?%s" % params, "{body}", headers)
response = conn.getresponse()
data = response.read()
tcdata = json.loads(data)
timecard_dict = {}
for timecard in tcdata:
tcid = timecard['Id']
timecard_dict.update({"Id": tcid})
for key in timecard_dict.keys():
id_no = str(timecard_dict['Id'])
tcurl = ("/time/timecard/" + id_no + "?%s")
conn = httplib.HTTPSConnection('****.azure-api.net')
conn.request("GET", tcurl % params, "{body}", headers)
response = conn.getresponse()
tcldata = response.read()
parsed = json.loads(tcldata)
print(json.dumps(parsed, indent=4, sort_keys=True))我尝试浏览这些数据并获得我想要的值,如下所示:
timepunches_dict = parsed
def employee_punch_data():
curr_date = None
for punch in timepunches_dict:
j = 1 if curr_date != punch['PunchInDateTime'][1:10] else j + 1
emp_id = punch['Employee']['EmployeeId']
emp_name = punch['Employee']['FullName']
curr_date = punch['PunchInDateTime'][1:10]
if punch['PunchInDateTime'] is not None:
punch_in_sql = punch['PunchInDateTime'].replace('T', ' ')
if punch['PunchOutDateTime'] is not None:
punch_out_sql = punch['PunchOutDateTime'].replace('T', ' ')
if today in punch_in_sql:
if j == 1:
print(emp_id, today, emp_name)
print(emp_id, today, j, punch_in_sql, punch_out_sql)
employee_punch_data()这个想法是获得一行数据,这些数据将被插入到标题表中,然后按照每次打卡和打卡出的顺序,在相应的日期执行每一次打卡。因为第一个集合是序列号1,下一个集合是序列号2,依此类推。我当前的输出是:
(u'080098', '2019-06-19', u'Kelly ****')
(u'080098', '2019-06-19', 1, u'2019-06-19 00:00:00', u'2019-06-19 04:27:06.677')
(u'080098', '2019-06-19', 2, u'2019-06-19 07:13:32.12', u'2019-06-19 12:13:00')但我希望得到的是:
(u'080098', '2019-06-19', u'Nicholas ****')
(u'080098', '2019-06-19', 1, u'2019-06-19 07:49:00', u'2019-06-19 14:49:00')
(u'080098', '2019-06-19', u'Test MBS')
(u'080098', '2019-06-19', 1, u'2019-06-19 09:49:00', u'2019-06-19 14:49:00')
(u'080098', '2019-06-19', u'Peter Quill')
(u'080098', '2019-06-19', 1, u'2019-06-19 08:00:00', u'2019-06-19 09:16:00')
(u'080098', '2019-06-19', 2, u'2019-06-19 13:43:00', u'2019-06-19 16:43:00')
(u'080098', '2019-06-19', 3, u'2019-06-19 17:21:00', u'2019-06-19 21:21:00')
(u'080098', '2019-06-19', u'Kelly ****')
(u'080098', '2019-06-19', 1, u'2019-06-19 00:00:00', u'2019-06-19 04:27:06.677')
(u'080098', '2019-06-19', 2, u'2019-06-19 07:13:32.12', u'2019-06-19 12:13:00')基本上,我的代码不是打印出这些值,而是将它们插入到我已经处理过的sql数据库中,我只需要获取它所需的数据。我哪里错了?我怀疑我可能覆盖了数据,只获得了最后一次输出,或者我的循环正在重置,以重新开始每个员工。我在哪里做错了?或者更好的问题是,这有可能吗?函数应该以某种方式在迭代循环中操作吗?很抱歉,这篇文章已经这么久了。
发布于 2019-06-28 03:49:14
以防任何人看到这个并想知道如何做,我让它工作,这是代码
conn = httplib.HTTPSConnection('buddypunch.azure-api.net')
conn.request("GET", "/timecard/GetByPayPeriod/525117?%s" % params, "{body}", headers)
response = conn.getresponse()
data = response.read()
tcdata = json.loads(data)
timecard_list = []
for timecard in tcdata:
tcid = str(timecard['Id'])
timecard_list.append(tcid)
for key in timecard_list:
tcurl = ("/time/timecard/" + key + "?%s")
conn = httplib.HTTPSConnection('buddypunch.azure-api.net')
conn.request("GET", tcurl % params, "{body}", headers)
response = conn.getresponse()
tcldata = response.read()
parsed = json.loads(tcldata)
timepunches_dict = parsed
curr_date = None
for punch in timepunches_dict:
j = 1 if curr_date != punch['PunchInDateTime'][1:10] else j + 1
emp_id = punch['Employee']['EmployeeId']
emp_name = punch['Employee']['FullName']
curr_date = punch['PunchInDateTime'][1:10]
punch_in_sql = punch['PunchInDateTime'].replace('T', ' ')
punch_out_sql = punch['PunchOutDateTime'].replace('T', ' ')
if today in punch_in_sql:
if j == 1:
# ONLY RUN FOR FIRST ITERATION
print(emp_id, today, emp_name)
# RUN FOR ALL ITERATIONS
print(emp_id, today, j, punch_in_sql, punch_out_sql)
conn.close()坦率地说,从timecard字典访问数据是一件很痛苦的事情,所以我只做了一个列表,并附加了单独的tcid,然后遍历该列表以从API获得数据。此外,我没有尝试运行一个函数,而是在现有循环中创建了一个for循环,这最终解决了问题,产生了我所期望的确切输出:
(u'080099', '2019-06-19', u'Nicholas ****')
(u'080099', '2019-06-19', 1, u'2019-06-19 07:49:00', u'2019-06-19 14:49:00')
(u'080100', '2019-06-19', u'Test MBS')
(u'080100', '2019-06-19', 1, u'2019-06-19 09:49:00', u'2019-06-19 14:49:00')
(u'080097', '2019-06-19', u'Peter Quill')
(u'080097', '2019-06-19', 1, u'2019-06-19 08:00:00', u'2019-06-19 09:16:00')
(u'080097', '2019-06-19', 2, u'2019-06-19 13:43:00', u'2019-06-19 16:43:00')
(u'080097', '2019-06-19', 3, u'2019-06-19 17:21:00', u'2019-06-19 21:21:00')
(u'080098', '2019-06-19', u'Kelly ****')
(u'080098', '2019-06-19', 1, u'2019-06-19 00:00:00', u'2019-06-19 04:27:06.677')
(u'080098', '2019-06-19', 2, u'2019-06-19 07:13:32.12', u'2019-06-19 12:13:00')https://stackoverflow.com/questions/56782030
复制相似问题