Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by completing interactive coding challenges and watching videos by expert instructors. Start Now!
This site is generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join 11 million other learners and get started learning Python for data science today!
Good news! You can save 25% off your Datacamp annual subscription with the code LEARNPYTHON23ALE25 - Click here to redeem your discount
सिरीयलाइज़ेशन
Python अंतर्निर्मित JSON पुस्तकालय प्रदान करता है JSON को एन्कोड और डिकोड करने के लिए।
Python 2.5 में, simplejson मॉड्यूल का उपयोग किया जाता है, जबकि Python 2.7 में json मॉड्यूल का उपयोग किया जाता है। चूंकि यह इंटरप्रेटर Python 2.7 का उपयोग करता है, इसलिए हम json का उपयोग करेंगे।
json मॉड्यूल का उपयोग करने के लिए, पहले इसे आयात किया जाना चाहिए:
JSON डेटा के दो बुनियादी प्रारूप हैं। या तो एक स्ट्रिंग में या ऑब्जेक्ट डेटा संरचना में। ऑब्जेक्ट डेटा संरचना, Python में, सूचियों और स्थानों से मिलकर बनी होती है जो एक-दूसरे के अंदर स्थित होती हैं। ऑब्जेक्ट डेटा संरचना एक को पायथन विधियाँ (सूचियों और स्थानों के लिए) का उपयोग करने की अनुमति देती है ताकि डेटा संरचना से तत्त्व जोड़, सूचीबद्ध, खोज और हटाया जा सके। स्ट्रिंग प्रारूप मुख्य रूप से डेटा को किसी अन्य प्रोग्राम में पास करने या डेटा संरचना में लोड करने के लिए उपयोग किया जाता है।
JSON को डेटा संरचना में वापस लोड करने के लिए, "loads" विधि का उपयोग करें। यह विधि एक स्ट्रिंग लेती है और इसे वापस JSON ऑब्जेक्ट डेटा संरचना में बदल देती है:
JSON को डेटा संरचना में एन्कोड करने के लिए, "dumps" विधि का उपयोग करें। यह विधि एक ऑब्जेक्ट लेती है और एक स्ट्रिंग लौटाती है:
Python एक Python स्वामित्व वाले डेटा अनुक्रमण विधि का समर्थन करता है जिसे pickle कहा जाता है (और एक तेज विकल्प जिसे cPickle कहा जाता है)।
आप इसका उपयोग बिल्कुल उसी तरह कर सकते हैं।
इस अभ्यास का उद्देश्य JSON स्ट्रिंग के साथ "Me" : 800 कुंजी-मूल्य जोड़ी जोड़ा गया प्रिंट करना है।
import json
# fix this function, so it adds the given name
# and salary pair to salaries_json, and return it
def add_employee(salaries_json, name, salary):
# Add your code here
return salaries_json
# test code
salaries = '{"Alfred" : 300, "Jane" : 400 }'
new_salaries = add_employee(salaries, "Me", 800)
decoded_salaries = json.loads(new_salaries)
print(decoded_salaries["Alfred"])
print(decoded_salaries["Jane"])
print(decoded_salaries["Me"])
import json
# fix this function, so it adds the given name
# and salary pair to salaries_json, and return it
def add_employee(salaries_json, name, salary):
salaries = json.loads(salaries_json)
salaries[name] = salary
return json.dumps(salaries)
# test code
salaries = '{"Alfred" : 300, "Jane" : 400 }'
new_salaries = add_employee(salaries, "Me", 800)
decoded_salaries = json.loads(new_salaries)
print(decoded_salaries["Alfred"])
print(decoded_salaries["Jane"])
print(decoded_salaries["Me"])
test_output_contains("300")
test_output_contains("400")
test_output_contains("800")
success_msg("Great work!")
This site is generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join over a million other learners and get started learning Python for data science today!