Last active
March 9, 2020 22:28
-
-
Save Tharwat96/11beafee94c465cb1ab6a2b2ae053288 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# write/overwrite file | |
f = open("text.txt", "w") | |
f.write("bla bla") | |
f.close() | |
# search for something in file | |
with open('data') as f: | |
if 'test' in f.read(): | |
print("true") | |
f.close() | |
# storing and restoring dictionaries | |
import json | |
with open('data.json', 'w') as fp: | |
json.dump(data, fp) | |
# Supply extra arguments like sort_keys or indent to get a pretty result. The argument sort_keys will sort the keys alphabetically and indent will indent your data structure with indent=N spaces. | |
json.dump(data, fp, sort_keys=True, indent=4) | |
JSON load: | |
with open('data.json', 'r') as fp: | |
data = json.load(fp) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment