# 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"
}
]
댓글 영역