2013-09-16 2 views
-1

안녕하세요, 다른 항목이 중첩 된 목록을 얻으려고합니다.공백 및 대괄호로 구분 된 문자열의 중첩 목록

'"Title" "Title of an article, chapter, etc." ("The Divine Comedy" "Rules of Construction") true null null false' 

이 내가 달성하기 위해 노력하고있어 결과입니다 : 이것은 내가 가지고있는 문자열입니다

def metadata(): 
    md = shlex.split(content) 
    print md 
: 나는 현재 shlex`사용하고

['Title', 'Title of an article, chapter, etc.', ['The Divine Comedy', 'Rules of Construction'], true, null, null, false] 

은,하지만 성공

답변

0

re.findall을 재귀 호출하면 트릭을 수행 할 수 있습니다. 당신은 신중하게

>>> st = '"Title" "Title of an article, chapter, etc." ("The Divine Comedy" "Rules of Construction") true null null false' 
>>> def nest_split(st): 
    return [nest_split(e[1:-1]) if e.startswith('(') else e.strip('"') for e in re.findall("\(.*?\)|\".*?\"|\w+", st)] 

>>> nest_split(st) 
['Title', 'Title of an article, chapter, etc.', ['The Divine Comedy', 'Rules of Construction'], 'true', 'null', 'null', 'false'] 

true, falsenull 그렇게 문자열

로 취급 될 것이다 유효한 파이썬 식별자가 아닌 정규식 패턴을 선택해야합니다