

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do I serialize a Python dictionary into a string and vice versa?
The best representation for a python dictionaty in string is JSON. you can use the json.dumps(dict) to convert the dictionary to string. And you can use json.loads(string) to get the dictionary back from the string.
Example
For example, we serialize given dictionary as follows.
>>> import json >>> d = {'id': 15, 'name': 'John'} >>> x = json.dumps(d) >>> print x {"id": 15, "name": "John"} >>> print json.loads(x) {u'id': 15, u'name': u'John'}
This is the easiest and fastest way to serialize and deserialize Python dictionaries.
- Related Questions & Answers
- How do I serialize a Python dictionary into a string, and then back to a dictionary?
- Java Program to Convert the ArrayList into a string and vice versa
- Convert string to DateTime and vice-versa in Python
- Python – Test String in Character List and vice-versa
- How to convert String to StringBuilder and vice versa Java?
- How to Convert a String to Hexadecimal and vice versa format in java?
- How do I format a string using a dictionary in Python 3?
- Converting string to number and vice-versa in C++
- Binary to decimal and vice-versa in Python
- How I can convert a Python Tuple into Dictionary?
- Adding Tuple to List and vice versa in Python
- How to convert a NumPy ndarray to a PyTorch Tensor and vice versa?
- Java Program to Convert the LinkedList into an Array and vice versa
- How do I convert a double into a string in C++?
- How to serialize Python dictionary to XML?
Advertisements