Python解析查询字符串列表 [英] Python parsing query string to list

查看:196
本文介绍了Python解析查询字符串列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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,我希望能够遍历视频时刻,但是似乎没有办法做到这一点。我试图在谷歌搜索图书馆,但我的谷歌福是今晚弱。



有什么建议吗?感谢!



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

  def add(root,path,value):
for path [: - 1]:$ b $ root root = root.setdefault(part,{} )
root [path [-1]] = value
$ b $ def parse(s):
items = {}
for key,parse_qsl(s) :
parts = filter(None,re.split('[\ [\]]',key))
name = parts [0]
如果名称不在项目中:
items [name] = {}
add(items [name],parts [1:],value)
return items

会产生一个哈希值:

$ p $ {'map':{ 'title':'orange'},'videos':{'1':{'moments':{'0':{'lat':'111','lng':'111','time' '''}},'type':'YouTube'},'0':{'moments':{'1':{'lat':'222','lng':'222','time' 222'},'0':{'lat':'111','lng':'111','ti我':'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
...


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



示例实现可以是:

pre> def add(root,path,value):
for path [: - 1]:$ b $ root root = root.setdefault(part,{})
root [路径[-1]] =值

def parse(s):
videos = {}
for key,parse_qsl(s)中的值:
parts =过滤器(None,re.split('[\ [\]]',key))
insert(videos,parts [1:],value)
返回视频


I have a form that submits data to the server that looks like the following:

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
...

I am using Flask and I would like to be able to loop through the videos and moments but it seems like there isn't a way to do that. I tried looking for libraries on Google but my Google-fu is weak tonight.

Any suggestions? Thanks!

EDIT: Based on lazy1's answer, I modified his/her code to

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

that will generate a hash:

{'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'}}}

for a query that looks like:

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
...

解决方案

You can use urlparse.parse_qsl to get the query parameters. However you'll need manually to construct the video objects.

Example implementation can be:

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

这篇关于Python解析查询字符串列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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