在 Python 中解析类似字典的 URL 参数 [英] Parsing dictionary-like URL parameters in Python

查看:25
本文介绍了在 Python 中解析类似字典的 URL 参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Python 实现服务器端过滤,以便为 KendoUI 的 Grid 组件提供服务.

I'm working on implementing server-side filtering to serve KendoUI's Grid component, using Python.

我面临的问题是它默认生成的 AJAX 调用似乎与 Flask 的内置 URL 解析器和 Python 的 urlparse 模块不兼容.

The problem I'm facing is that the AJAX call that it generates by default seems to be incompatible with both Flask's built-in URL parser and Python's urlparse module.

这是我遇到问题的查询字符串类型的人为示例:a=b&c=d&foo[bar]=baz&foo[baz]=qis&foo[qis]=条形

Here's a contrived sample of the type of query string I'm having trouble with: a=b&c=d&foo[bar]=baz&foo[baz]=qis&foo[qis]=bar

这是我想要的结果:

{
    'a': 'b',
    'c': 'd',
    'foo': {
        'bar': 'baz',
        'baz': 'qis',
        'qis': bar'
    }
}

不幸的是,这是您从中获得的 request.args,如果传递给 Flask 端点:

Unfortunately, here's the request.args you get from this, if passed to a Flask endpoint:

{
    'a': 'b',
    'c': 'd',
    'foo[bar]': 'baz'
    'foo[baz]': 'qis'
    'foo[qis]': 'bar'
}

更糟糕的是,在实践中,该结构可能有几层深.将列 foo 过滤为仅值等于 'bar' 的行的基本调用将产生以下结果:

Worse yet, in practice, the structure can be several layers deep. A basic call where you're filtering the column foo to only rows where the value is equal to 'bar' will produce the following:

{
    'filter[logic]': 'and',
    'filter[filters][0][value]': 'bar',
    'filter[filters][0][field]': 'foo',
    'filter[filters][0][operator]': 'eq'
}

我检查了 RFC,它要求查询字符串只包含非分层"数据.虽然我相信它指的是 URI 所代表的对象,但在我能找到的规范中并没有规定这种类型的数据结构.

I checked the RFC, and it requires that the query string contain only "non-hierarchical" data. While I believe it's referring to the object the URI represents, there is no provision for this type of data structure in the specification that I can find.

我开始编写一个函数,它接受一个参数字典并返回它们所代表的嵌套结构,但我很快意识到这是一个微妙的问题,而且肯定有人以前遇到过这个问题.

I begin to write a function that would take a dictionary of params and return the nested construct they represented, but I soon realized that it was nuanced problem, and that surely someone out there has had this trouble before.

有没有人知道可以按照我想要的方式解析这些参数的模块,或者我可能忽略的解析它们的优雅方式?

Is anyone aware of either a module that will parse these parameters in the way I'm wanting, or an elegant way to parse them that I've perhaps overlooked?

推荐答案

我只是写了一个小函数来做到这一点:

I just wrote a little function to do this:

from collections import defaultdict
import re
params = {
    'a': 'b',
    'c': 'd',
    'foo[bar]': 'element1',
    'foo[baz]': 'element2',
    'foo[qis]': 'element3',
    'foo[borfarglan][bofgl]': 'element4',
    'foo[borfarglan][bafgl]': 'element5',
}

def split(string, brackets_on_first_result = False):
    matches = re.split("[[]]+", string)
    matches.remove('')
    return matches

def mr_parse(params):
    results = {}
    for key in params:
        if '[' in key:
            key_list = split(key)
            d = results
            for partial_key in key_list[:-1]:
                if partial_key not in d:
                    d[partial_key] = dict()
                d = d[partial_key]
            d[key_list[-1]] = params[key]
        else:
            results[key] = params[key]
    return results
print mr_parse(params)

这应该适用于任何嵌套级别.

This should work to any nest level.

这篇关于在 Python 中解析类似字典的 URL 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆