我得到的JSON數據提交給我的api(使用Flask構建)端點,我需要將該數據導出爲excel,但問題是一旦我的api收到數據並將其加載到請求中。 json數據的「正文」部分被加密 - 不是以同樣的順序收到它,它完全打破了我的表格標題,更不用說需要的輸出excel格式。flask request.json命令
包括下面我收到我的API
{
"data": {
"header": [
"date,group.groupname,user.NameSurname,forwarding_number,reciver_number,CallerNameSurname,alert,connection,call_summ"
],
"body": [
{
"date": "2013-01-08",
"group.groupname": "customerService",
"user.NameSurname": "Romāns Tiščenko",
"forwarding_number": "66055002",
"reciver_number": "66055002",
"CallerNameSurname": false,
"alert": "00:00:14",
"connection": "00:00:53",
"call_summ": "00:01:07"
},
{
"date": "2013-01-08",
"group.groupname": "customerService",
"user.NameSurname": "Romāns Tiščenko",
"forwarding_number": "66055002",
"reciver_number": "66055002",
"CallerNameSurname": false,
"alert": "00:00:27",
"connection": "00:00:25",
"call_summ": "00:00:52"
},
{
"date": "2013-01-08",
"group.groupname": "customerService",
"user.NameSurname": "Romāns Tiščenko",
"forwarding_number": "66055002",
"reciver_number": "66055002",
"CallerNameSurname": false,
"alert": "00:00:16",
"connection": "00:01:00",
"call_summ": "00:01:16"
},
{
"date": "2013-01-08",
"group.groupname": "customerService",
"user.NameSurname": "Arvīds Boļesko",
"forwarding_number": "66055001",
"reciver_number": "66055001",
"CallerNameSurname": false,
"alert": "00:00:22",
"connection": "00:00:52",
"call_summ": "00:01:14"
},
{
"date": "2013-01-08",
"group.groupname": "customerService",
"user.NameSurname": "Romāns Tiščenko",
"forwarding_number": "66055002",
"reciver_number": "66055002",
"CallerNameSurname": false,
"alert": "00:00:18",
"connection": "00:00:09",
"call_summ": "00:00:27"
},
{
"date": "2013-01-08",
"group.groupname": "customerService",
"user.NameSurname": "Romāns Tiščenko",
"forwarding_number": "66055002",
"reciver_number": "66055002",
"CallerNameSurname": false,
"alert": "00:00:09",
"connection": "00:01:59",
"call_summ": "00:02:08"
},
{
"date": "2013-01-08",
"group.groupname": "customerService",
"user.NameSurname": "Romāns Tiščenko",
"forwarding_number": "66055002",
"reciver_number": "66055002",
"CallerNameSurname": false,
"alert": "00:00:08",
"connection": "00:02:14",
"call_summ": "00:02:22"
},
{
"date": "2013-01-08",
"group.groupname": "customerService",
"user.NameSurname": "Roberts Neijs",
"forwarding_number": "66055003",
"reciver_number": "66055003",
"CallerNameSurname": false,
"alert": "00:00:04",
"connection": "00:00:01",
"call_summ": "00:00:05"
},
{
"date": "2013-01-08",
"group.groupname": "customerService",
"user.NameSurname": "Arvīds Boļesko",
"forwarding_number": "66055001",
"reciver_number": "66055001",
"CallerNameSurname": false,
"alert": "00:00:57",
"connection": "00:01:26",
"call_summ": "00:02:23"
},
{
"date": "2013-01-08",
"group.groupname": "customerService",
"user.NameSurname": "Roberts Neijs",
"forwarding_number": "66055003",
"reciver_number": "66055003",
"CallerNameSurname": false,
"alert": "00:00:05",
"connection": "00:02:23",
"call_summ": "00:02:28"
},
{
"date": "2013-01-08",
"group.groupname": "customerService",
"user.NameSurname": "Roberts Neijs",
"forwarding_number": "66055003",
"reciver_number": "66055003",
"CallerNameSurname": false,
"alert": "00:00:06",
"connection": "00:00:44",
"call_summ": "00:00:50"
},
{
"date": "2013-01-08",
"group.groupname": "customerService",
"user.NameSurname": "Romāns Tiščenko",
"forwarding_number": "66055002",
"reciver_number": "66055002",
"CallerNameSurname": false,
"alert": "00:00:08",
"connection": "00:02:43",
"call_summ": "00:02:51"
},
{
"date": "2013-01-08",
"group.groupname": "customerService",
"user.NameSurname": "Roberts Neijs",
"forwarding_number": "66055003",
"reciver_number": "66055003",
"CallerNameSurname": false,
"alert": "00:00:05",
"connection": "00:01:13",
"call_summ": "00:01:18"
},
{
"date": "2013-01-08",
"group.groupname": "customerService",
"user.NameSurname": "Roberts Neijs",
"forwarding_number": "66055003",
"reciver_number": "66055003",
"CallerNameSurname": false,
"alert": "00:00:09",
"connection": "00:00:01",
"call_summ": "00:00:10"
},
{
"date": "2013-01-08",
"group.groupname": "administration",
"user.NameSurname": "Ieva Sproģe",
"forwarding_number": "67783165",
"reciver_number": "67783165",
"CallerNameSurname": false,
"alert": "00:00:06",
"connection": "00:01:30",
"call_summ": "00:01:36"
},
{
"date": "2013-01-08",
"group.groupname": "customerService",
"user.NameSurname": "Roberts Neijs",
"forwarding_number": "66055003",
"reciver_number": "66055003",
"CallerNameSurname": false,
"alert": "00:00:06",
"connection": "00:00:50",
"call_summ": "00:00:56"
},
{
"date": "2013-01-08",
"group.groupname": "administration",
"user.NameSurname": "Ieva Sproģe",
"forwarding_number": "67783165",
"reciver_number": "67783165",
"CallerNameSurname": false,
"alert": "00:00:04",
"connection": "00:02:00",
"call_summ": "00:02:04"
},
{
"date": "2013-01-08",
"group.groupname": "customerService",
"user.NameSurname": "Roberts Neijs",
"forwarding_number": "66055003",
"reciver_number": "66055003",
"CallerNameSurname": false,
"alert": "00:00:07",
"connection": "00:00:26",
"call_summ": "00:00:33"
},
{
"date": "2013-01-08",
"group.groupname": "administration",
"user.NameSurname": "Ieva Sproģe",
"forwarding_number": "67783165",
"reciver_number": "67783165",
"CallerNameSurname": false,
"alert": "00:00:04",
"connection": "00:00:13",
"call_summ": "00:00:17"
}
],
"footer": [
",,,,,,7,8,9"
],
"filename": "outGoingAnswered.xls"
}
}
正如我所說的原始JSON數據,一旦這樣每個「體」的「行」內讀入API與request.json數據亂七八糟 - 訂單被打破。它應該和標題中所寫的一樣。
我試圖加載它使用不同的json庫,但結果是相同的,加載的數據不是在相同的順序,因爲它給出。我完全無能爲力,爲什麼它會以這種方式發生,這讓我的生活變得非常困難,因爲我對Python比較陌生。
我應該提到,我無法將鍵匹配到標題列,因爲標題可能比「body」「row」中的鍵少。
舉個例子輸出我可以提供略微不同的「炒版」發佈JSON數據:
{
"data": {
"body": [
{
"group.groupname": "customerService",
"connection": "00:01:28",
"call_summ": "00:01:42",
"CallerNameSurname": "false",
"forwarding_number": "66055002",
"user.NameSurname": "Arnolds Apels",
"date": "2012-12-13",
"reciver_number": "66055002",
"alert": "00:00:14"
},
{
"group.groupname": "customerService",
"connection": "00:01:33",
"call_summ": "00:01:45",
"CallerNameSurname": "false",
"forwarding_number": "66055002",
"user.NameSurname": "Arnolds Apels",
"date": "2012-12-13",
"reciver_number": "66055002",
"alert": "00:00:12"
},
{
"group.groupname": "administration",
"connection": "00:00:30",
"call_summ": "00:00:33",
"CallerNameSurname": "false",
"forwarding_number": "67783165",
"user.NameSurname": "Ieva Sproģe",
"date": "2012-12-13",
"reciver_number": "67783165",
"alert": "00:00:03"
}
],
"header": [
"date,group.groupname,user.NameSurname,forwarding_number,reciver_number,CallerNameSurname,alert,connection,call_summ"
],
"footer": [
"false,false,user.NameSurname,forwarding_number,reciver_number,CallerNameSurname,alert,connection,call_summ"
],
"filename": [
"asdasda.xls"
]
}
}
和良好的措施增加了Excel導出代碼:
r = request.json
data = r['data']
if data:
# Initialize excel export
excel = Workbook()
sheet = excel.add_sheet('Report')
## Excel headers
# Setup font styling
font = Font()
font.name = 'Arial'
font.bold = True
# Setup cell border styling
borders = Borders()
borders.bottom = 1
borders.right = 1
# Setup formating style
style = XFStyle()
style.font = font
style.borders = borders
# Write headers
header = data['header'][0]
headers = header.split(',')
h = 0
for col in headers:
if col == 'false':
col = ''
sheet.write(0, h, col, style)
h += 1
# Write body
rows = data['body']
i = 1
for row in rows:
x = sheet.row(i)
y = 0
for key, value in row.iteritems():
x.write(y, value)
y += 1
i += 1
# Write footer
footer = data['footer'][0]
footers = footer.split(',')
f = 0
for col in footers:
if col == 'false':
col = ''
sheet.write(i, f, col)
f += 1
# Setup column widths
if h > f:
cols = h
else:
cols = f
a = 0
while a < cols:
sheet.col(a).width = 5000
a += 1
# Get report filename
filename = data['filename'][0]
# Setup report path
path = "/path/to/exports/" + filename
# Save the report
excel.save(path)
哦,我的上帝,我覺得不正確讀取蟒蛇真的很傻字典文件:(我現在要做壽,感謝很多的瘋狂快速響應最終我!因爲我無法控制發佈到API的內容,所以停留在第二個版本。 –
我還沒有測試過,但這個答案可能對您有所幫助http://stackoverflow.com/a/6921760/1154535 – Smoe