【问题标题】:Python parsing query string to listPython将查询字符串解析为列表
【发布时间】:2011-07-14 02:04:14
【问题描述】:

我有一个向服务器提交数据的表单,如下所示:

videos[0][type]=Vimeo&
  videos[0][moments][0][time]=11&
  videos[0][moments][0][lng]=111&
  videos[0][moments][0][lat]=111&
  videos[0][moments][1][time]=222&
  videos[0][moments][1][lng]=222&
  videos[0][moments][1][lat]=222&
videos[1][type]=YouTube&
  videos[1][moments][0][time]=111&
  videos[1][moments][0][lng]=111&
  videos[1][moments][0][lat]=111
...

我正在使用 Flask,我希望能够遍历 videosmoments,但似乎没有办法做到这一点。我尝试在 Google 上查找库,但我的 Google-fu 今晚很弱。

有什么建议吗?谢谢!

编辑:根据lazy1的回答,我将他/她的代码修改为

def add(root, path, value):
  for part in path[:-1]:
    root = root.setdefault(part, {})
  root[path[-1]] = value

def parse(s):
  items = {}
  for key, value in parse_qsl(s):
    parts = filter(None, re.split('[\[\]]', key))
    name = parts[0]
    if name not in items: 
      items[name] = {}
    add(items[name], parts[1:], value)
  return items

这将生成一个哈希:

{'map': {'title': 'orange'}, 'videos': {'1': {'moments': {'0': {'lat': '111', 'lng': '111', 'time': '111'}}, 'type': 'YouTube'}, '0': {'moments': {'1': {'lat': '222', 'lng': '222', 'time': '222'}, '0': {'lat': '111', 'lng': '111', 'time': '11'}}, 'type': 'Vimeo'}}}

对于看起来像这样的查询:

map[title]=orange&
videos[0][type]=Vimeo&
  videos[0][moments][0][time]=11&
  videos[0][moments][0][lng]=111&
  videos[0][moments][0][lat]=111&
  videos[0][moments][1][time]=222&
  videos[0][moments][1][lng]=222&
  videos[0][moments][1][lat]=222&
videos[1][type]=YouTube&
  videos[1][moments][0][time]=111&
  videos[1][moments][0][lng]=111&
  videos[1][moments][0][lat]=111
...

【问题讨论】:

    标签: python flask


    【解决方案1】:

    您可以使用urlparse.parse_qsl 获取查询参数。但是,您需要手动构建视频对象。

    示例实现可以是:

    def add(root, path, value):
        for part in path[:-1]:
            root = root.setdefault(part, {})
        root[path[-1]] = value
    
    def parse(s):
        videos = {}
        for key, value in parse_qsl(s):
            parts = filter(None, re.split('[\[\]]', key))
            insert(videos, parts[1:], value)
        return videos
    

    【讨论】:

      【解决方案2】:

      如果您使用formencode 并且可以将您的密钥格式更改为:

      map.title=orange&
      videos-0.type=Vimeo&
        videos-0.moments-0.time=11&
        videos-0.moments-0.lng=111&
        videos-0.moments-0.lat=111&
        videos-0.moments-1.time=222&
        videos-0.moments-1.lng=222&
        videos-0.moments-1.lat=222&
      videos-1.type=YouTube&
        videos-1.moments-0.time]=111&
        videos-1.moments-0.lng]=111&
        videos-1.moments-0.lat]=111
      

      你可以使用:

      from urlparse import parse_qsl
      from formencode.variabledecode import variable_decode
      
      def parse(s):
         return variable_decode(parse_qsl(s))
      

      给予:

      {
       'map': {'title': 'orange'}, 
       'videos': [ 
         {
           'moments': [ {'lat': '111', 'lng': '111', 'time': '11'}, 
                        {'lat': '222', 'lng': '222', 'time': '222'}],
           'type': 'Vimeo'
          }, {
          'moments': [ {'lat': '111', 'lng': '111', 'time': '111'} ],
      
          'type': 'YouTube'
         }
       ]
      }
      

      【讨论】:

      • 谢谢!我想我会使用那个库。我不喜欢名称的格式,但我想我可以接受它。 :)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-20
      • 1970-01-01
      • 2011-09-19
      • 2011-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多