Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python flask request.json订单_Python_Json_Python 2.7_Flask - Fatal编程技术网

Python flask request.json订单

Python flask request.json订单,python,json,python-2.7,flask,Python,Json,Python 2.7,Flask,我将JSON数据提交到我的api(使用Flask构建)端点,我需要将该数据导出到excel,但问题是,一旦我的api接收到数据并使用request.JSON将其加载到“body”部分数据被打乱了-与我收到数据的顺序不同,这完全打破了我的表格标题,更不用说所需的输出excel格式了 下面是我接收到的原始JSON数据 { "data": { "header": [ "date,group.groupname,user.NameSurname,forwarding_number

我将JSON数据提交到我的api(使用Flask构建)端点,我需要将该数据导出到excel,但问题是,一旦我的api接收到数据并使用request.JSON将其加载到“body”部分数据被打乱了-与我收到数据的顺序不同,这完全打破了我的表格标题,更不用说所需的输出excel格式了

下面是我接收到的原始JSON数据

{
"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"
}
}
正如我所说的,一旦使用request.json将其读入api,“body”的每个“行”中的数据都被置乱——顺序被打破。它应该写在标题中

我尝试使用不同的json库加载它,但结果是一样的,加载的数据顺序与给定的顺序不同。我完全不知道为什么会这样,这让我的生活变得非常困难,因为我对Python比较陌生

我应该提到,我无法将键与标题列匹配,因为标题的键可能比“body”行中的键少

作为示例输出,我可以提供稍微不同的发布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)
r=request.json
data=r['data']
如果数据:
#初始化excel导出
excel=工作簿()
工作表=excel。添加工作表(“报告”)
##Excel标题
#设置字体样式
font=font()
font.name='Arial'
font.bold=True
#设置单元格边框样式
边框=边框()
borders.bottom=1
borders.right=1
#设置格式化样式
style=XFStyle()
style.font=font
style.borders=边框
#写标题
header=数据['header'][0]
headers=header.split(“,”)
h=0
对于标题中的列:
如果col==“false”:
col=''
书写(0,h,col,style)
h+=1
#正文
行=数据['body']
i=1
对于行中的行:
x=第页第(i)行
y=0
对于键,第行中的值。iteritems()
x、 写入(y,值)
y+=1
i+=1
#写页脚
页脚=数据['footer'][0]
footers=footer.split(',')
f=0
对于页脚中的列:
如果col==“false”:
col=''
书写(i、f、col)
f+=1
#设置列宽
如果h>f:
cols=h
其他:
cols=f
a=0
而a
Python字典和JSON对象都是无序的。你看到的行为是正常的

如果需要保留顺序,请不要使用
dict()
,而是使用元组列表:

   [
       ["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"]
   ],
   # ... etc.
或者您也可以使用
data['header'][0]
字符串按顺序读取正文
dict()
值,因为已经为您排序了:

rows = data['body']
i = 1
for row in rows:
    x = sheet.row(i)
    y = 0
    for col in headers:
        x.write(y, row.get(col, ''))
        y += 1
    i += 1

哦,我的天,我觉得没有正确阅读pythons dict文档真的很愚蠢:(现在我读了tho,非常感谢您的快速响应!最后我坚持使用第二个版本,因为我无法控制发布到API的内容。我还没有测试它,但这个答案可能对您有所帮助