199. Data Serialization
1. JSON Serialization
import json
data = {"name": "Alice", "age": 30, "city": "New York"}
# Convert Python object to JSON string
json_data = json.dumps(data)
print(json_data)
# Save to a file
with open('data.json', 'w') as f:
json.dump(data, f)2. JSON Deserialization
import json
# JSON string
json_data = '{"name": "Alice", "age": 30, "city": "New York"}'
# Convert JSON string to Python object
data = json.loads(json_data)
print(data)
# Read from a file
with open('data.json', 'r') as f:
data_from_file = json.load(f)
print(data_from_file)3. XML Serialization with xml.etree.ElementTree
xml.etree.ElementTree4. XML Deserialization with xml.etree.ElementTree
xml.etree.ElementTree5. Pickling: Binary Serialization with pickle
pickle6. Custom Serialization with pickle
pickle7. YAML Serialization with PyYAML
PyYAML8. YAML Deserialization with PyYAML
PyYAML9. MessagePack: Efficient Binary Serialization
10. Custom JSON Serialization with json
jsonConclusion:
Last updated