mirror of
https://github.com/kennethreitz/python-guide.git
synced 2026-06-05 23:00:18 +00:00
Revert "Add info on working with file objects"
This reverts commit 2e2d70ca9b.
This commit is contained in:
@@ -39,36 +39,6 @@ You can also convert the following to JSON:
|
||||
print(json.dumps(d))
|
||||
'{"first_name": "Guido", "last_name": "Rossum", "titles": ["BDFL", "Developer"]}'
|
||||
|
||||
Working with File Objects
|
||||
-------------------------
|
||||
|
||||
We can also load a JSON file by using ``json.load`` instead of ``json.loads``:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
import json
|
||||
|
||||
with file('path/to/file.json') as f:
|
||||
loaded_json = json.load(f)
|
||||
|
||||
print(loaded_json) # {'first_name': 'Ada', 'last_name': 'Lovelace'}
|
||||
|
||||
Here's an example of writing directly to a file by using ``json.dump`` instead of ``json.dumps``:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
import json
|
||||
|
||||
my_data = {
|
||||
'name': 'Alan Turing',
|
||||
'played_by': 'Benedict Cumberbatch'
|
||||
}
|
||||
|
||||
with file('path/to/file.json', 'w') as f:
|
||||
json.dump(my_data, f)
|
||||
|
||||
``path/to/file.json`` now contains a JSON representation of the my_data dictionary.
|
||||
|
||||
|
||||
simplejson
|
||||
----------
|
||||
|
||||
Reference in New Issue
Block a user