티스토리 뷰

Python

xml, json

techbard 2016.05.02 18:19

# xml generator


from xml.etree.ElementTree import Element, SubElement, tostring


root = Element('rebels')

rebel = SubElement(root, 'rebel')


firstName = SubElement(rebel, 'firstName')

lastName = SubElement(rebel, 'lastName')


firstName.text = 'Luke'

lastName.text = 'Skywalker'


print(tostring(root))


# 결과

b'<rebels><rebel><firstName>Luke</firstName><lastName>Skywalker</lastName></rebel></rebels>'


# json generator


import json


straightJSON = json.dumps(['rebles', 

{'firstName': 'Luke', 'lastName': 'Skywalker'}],

separators = (',', ':'))


print("straightJSON Output:\n", straightJSON)


prettyJSON = json.dumps(['rebels',

{'firstName': 'Luke', 'lastName': 'Skywalker'}],

sort_keys=True,

indent = 4)


print("preetyJSON Output:\n", prettyJSON)


# 결과

straightJSON Output:

 ["rebles",{"firstName":"Luke","lastName":"Skywalker"}]

preetyJSON Output:

 [

    "rebels",

    {

        "firstName": "Luke",

        "lastName": "Skywalker"

    }

]


댓글
댓글쓰기 폼
공지사항
Total
407,535
Today
9
Yesterday
19
«   2019/09   »
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          
글 보관함