티스토리 뷰
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | # jsonParser.py # # Implementation of a simple JSON parser, returning a hierarchical # ParseResults object support both list- and dict-style data access. # # Copyright 2006, by Paul McGuire # # Updated 8 Jan 2007 - fixed dict grouping bug, and made elements and # members optional in array and object collections # # Updated 9 Aug 2016 - use more current pyparsing constructs/idioms # json_bnf = """ object { members } {} members string : value members , string : value array [ elements ] [] elements value elements , value value string number object array true false null """ from pyparsing import * def make_keyword(kwd_str, kwd_value): return Keyword(kwd_str).setParseAction(replaceWith(kwd_value)) TRUE = make_keyword("true", True) FALSE = make_keyword("false", False) NULL = make_keyword("null", None) LBRACK, RBRACK, LBRACE, RBRACE, COLON = map(Suppress, "[]{}:") jsonString = dblQuotedString().setParseAction(removeQuotes) jsonNumber = pyparsing_common.number() jsonObject = Forward() jsonValue = Forward() jsonElements = delimitedList( jsonValue ) jsonArray = Group(LBRACK + Optional(jsonElements, []) + RBRACK) jsonValue << (jsonString | jsonNumber | Group(jsonObject) | jsonArray | TRUE | FALSE | NULL) memberDef = Group(jsonString + COLON + jsonValue) jsonMembers = delimitedList(memberDef) jsonObject << Dict(LBRACE + Optional(jsonMembers) + RBRACE) jsonComment = cppStyleComment jsonObject.ignore(jsonComment) if __name__ == "__main__": testdata = """ { "glossary": { "title": "example glossary", "GlossDiv": { "title": "S", "GlossList": { "ID": "SGML", "SortAs": "SGML", "GlossTerm": "Standard Generalized Markup Language", "TrueValue": true, "FalseValue": false, "Gravity": -9.8, "LargestPrimeLessThan100": 97, "AvogadroNumber": 6.02E23, "EvenPrimesGreaterThan2": null, "PrimesLessThan10" : [2,3,5,7], "Acronym": "SGML", "Abbrev": "ISO 8879:1986", "GlossDef": "A meta-markup language, used to create markup languages such as DocBook.", "GlossSeeAlso": ["GML", "XML", "markup"], "EmptyDict" : {}, "EmptyList" : [] } } } } """ import pprint results = jsonObject.parseString(testdata) pprint.pprint( results.asList() ) print() def testPrint(x): print(type(x),repr(x)) print(list(results.glossary.GlossDiv.GlossList.keys())) testPrint( results.glossary.title ) testPrint( results.glossary.GlossDiv.GlossList.ID ) testPrint( results.glossary.GlossDiv.GlossList.FalseValue ) testPrint( results.glossary.GlossDiv.GlossList.Acronym ) testPrint( results.glossary.GlossDiv.GlossList.EvenPrimesGreaterThan2 ) testPrint( results.glossary.GlossDiv.GlossList.PrimesLessThan10 ) | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | [['glossary', [['title', 'example glossary'], ['GlossDiv', [['title', 'S'], ['GlossList', [['ID', 'SGML'], ['SortAs', 'SGML'], ['GlossTerm', 'Standard Generalized Markup Language'], ['TrueValue', True], ['FalseValue', False], ['Gravity', -9.8], ['LargestPrimeLessThan100', 97], ['AvogadroNumber', 6.02e+23], ['EvenPrimesGreaterThan2', None], ['PrimesLessThan10', [2, 3, 5, 7]], ['Acronym', 'SGML'], ['Abbrev', 'ISO 8879:1986'], ['GlossDef', 'A meta-markup language, used to create markup languages such as DocBook.'], ['GlossSeeAlso', ['GML', 'XML', 'markup']], ['EmptyDict', []], ['EmptyList', [[]]]]]]]]]] () ['GlossSeeAlso', 'GlossDef', 'Abbrev', 'EmptyList', 'Acronym', 'GlossTerm', 'TrueValue', 'Gravity', 'ID', 'SortAs', 'PrimesLessThan10', 'AvogadroNumber', 'EvenPrimesGreaterThan2', 'LargestPrimeLessThan100', 'FalseValue', 'EmptyDict'] (<type 'str'>, "'example glossary'") (<type 'str'>, "'SGML'") (<type 'bool'>, 'False') (<type 'str'>, "'SGML'") (<type 'NoneType'>, 'None') (<class 'pyparsing.ParseResults'>, '([2, 3, 5, 7], {})') | cs |
'프로그래밍' 카테고리의 다른 글
airspeed를 사용한 템플릿 설정 관리 (0) | 2016.10.19 |
---|---|
pyparsing을 사용한 bacula 설정 파서 (0) | 2016.10.19 |
PHP Restful API Silex + Swagger UI (0) | 2016.10.13 |
Python 이미지를 스트링으로 변환 (0) | 2016.09.02 |
Python 리소스 제한 (0) | 2016.08.10 |
댓글
warpmemory
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
페이지
TAG
- Web
- Linux
- engineering
- command
- monitoring
- deview
- example
- 예제
- Module
- PowerShell
- File
- Ansible
- check
- httpd
- code
- 명령어
- MariaDB
- RESTful
- client
- 코드
- limits
- apache
- 번역
- mysql
- Python
- error
- Windows
- configuration
- 이슈처리
- 외부링크
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
글 보관함