我是python的新手,正在寻找一种优雅的方式来完成以下工作。
我有一个字符串说:
s = u'(鞋子="AAA", last = "BBB", abcd)'
我正在考虑一个函数,它可以解析上面的字符串并以下列格式给出输出。
arg, kwarg = foo(s)
def foo():
# the implementation I dont know.
我如何在python中执行此操作?
答案 0 :(得分:2)
解析遵循某些语法规则的字符串的一种很好的方法是第三方pyparsing库。这是非常通用的,缺少允许用户输入的正式语法定义:
#coding:utf8
from pyparsing import *
# Names for symbols
_lparen = Suppress('(')
_rparen = Suppress(')')
_quote = Suppress('"')
_eq = Suppress('=')
# Parsing grammar definition
data = (_lparen + # left parenthesis
delimitedList( # Zero or more comma-separated items
Group( # Group the contained unsuppressed tokens in a list
Regex(u'[^=,)\s]+') + # Grab everything up to an equal, comma, endparen or whitespace as a token
Optional( # Optionally...
_eq + # match an =
_quote + # a quote
Regex(u'[^"]*') + # Grab everything up to another quote as a token
_quote) # a quote
) # EndGroup - will have one or two items.
) + # EndList
_rparen) # right parenthesis
def process(s):
items = data.parseString(s).asList()
args = [i[0] for i in items if len(i) == 1]
kwargs = {i[0]:i[1] for i in items if len(i) == 2}
return args,kwargs
s = u'(鞋子="AAA", last = "BBB", abcd)'
args,kwargs = process(s)
for a in args:
print a
for k,v in kwargs.items():
print k,v
输出:
abcd
鞋子 AAA
last BBB
答案 1 :(得分:0)
def foo(s):
data={"kwargs":[],"args":[]}
for item in s:
if "=" in item: data['kwargs'].append(item)
else: data['args'].append(item)
return data
s = u'(鞋子="AAA", last = "BBB", abcd)'
s = s[1:-1] # get rid of the parenthesis
print foo(s)