Import a exported dict into python

YiFei

I'm writing something like a database, but rather simple and small. So I used a dict, which is then saved in a file.

The code is roughly:

d = {'apple': 1, 'bear': 2}
print(d, file=f)

I want it to be imported the next time I run it, i.e. import it as a dictionary from file, anyway to do it?

Casimir Crystal

If you'd like save some data such as list, dict or tuple, etc. to a file. And you want to edit them or you just want them be readable . Use json module like this:

>>> import json
>>> d = {'apple': 1, 'bear': 2}

>>> print(d)
{'bear': 2, 'apple': 1}

>>> print(json.dumps(d))
{"bear": 2, "apple": 1}  # these are json data
>>> 

Now you could save these data to a file. If you want to load them, use json.loads() like this:

>>> json_data = '{"bear": 2, "apple": 1}'
>>> d = json.loads(json_data)
>>> d['bear']
2
>>>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Import a file as a dict() in Python 3

From Dev

Import CSP blob exported from .NET into python pyCrypto

From Dev

Is it possible to import a Python module (.py file) into a dict?

From Dev

Is it possible to import a Python module (.py file) into a dict?

From Dev

cant import an exported file into wampserver

From Dev

Import python dict with variable-length list items into pandas

From Java

Import two exported classes with the same name

From Dev

Unable to import class that was exported two times (Typescript)

From Dev

How to import a previously exported Ubuntu tarball in WSL?

From Dev

Dict into Dict Python

From Dev

Import JSON dict with list into Dataframe

From Dev

Python: import csv files, set dict name as filename, headers as keys, and columns as values

From Dev

Converting a dict to a dict of lists in Python

From Dev

How to import a function exported using this given export construct?

From Dev

Attempted import error: 'default' is not exported from '../assertThisInitialized/_index.mjs'

From Dev

Attempted import error: 'default' is not exported from '../assertThisInitialized/_index.mjs'

From Dev

Attempted import error: 'getMoviesList' is not exported from './actions'; error in react redux

From Dev

Exported ova failed to import in xcp-ng center

From Dev

How to import text file with sessions (that I have earlier exported) into fiddler?

From Dev

How can I import multiple mysql queries exported as a text with php?

From Dev

python import within import

From Dev

Python program working in CMD but not when exported to .exe

From Dev

testing non-exported methods in python

From Java

How can I import a json as a dict?

From Dev

Import dict without name from a module

From Dev

Import dict without name from a module

From Java

Deep copy of a dict in python

From Dev

python dict() initialisation

From Java

Python checking dict keys

Related Related

  1. 1

    Import a file as a dict() in Python 3

  2. 2

    Import CSP blob exported from .NET into python pyCrypto

  3. 3

    Is it possible to import a Python module (.py file) into a dict?

  4. 4

    Is it possible to import a Python module (.py file) into a dict?

  5. 5

    cant import an exported file into wampserver

  6. 6

    Import python dict with variable-length list items into pandas

  7. 7

    Import two exported classes with the same name

  8. 8

    Unable to import class that was exported two times (Typescript)

  9. 9

    How to import a previously exported Ubuntu tarball in WSL?

  10. 10

    Dict into Dict Python

  11. 11

    Import JSON dict with list into Dataframe

  12. 12

    Python: import csv files, set dict name as filename, headers as keys, and columns as values

  13. 13

    Converting a dict to a dict of lists in Python

  14. 14

    How to import a function exported using this given export construct?

  15. 15

    Attempted import error: 'default' is not exported from '../assertThisInitialized/_index.mjs'

  16. 16

    Attempted import error: 'default' is not exported from '../assertThisInitialized/_index.mjs'

  17. 17

    Attempted import error: 'getMoviesList' is not exported from './actions'; error in react redux

  18. 18

    Exported ova failed to import in xcp-ng center

  19. 19

    How to import text file with sessions (that I have earlier exported) into fiddler?

  20. 20

    How can I import multiple mysql queries exported as a text with php?

  21. 21

    python import within import

  22. 22

    Python program working in CMD but not when exported to .exe

  23. 23

    testing non-exported methods in python

  24. 24

    How can I import a json as a dict?

  25. 25

    Import dict without name from a module

  26. 26

    Import dict without name from a module

  27. 27

    Deep copy of a dict in python

  28. 28

    python dict() initialisation

  29. 29

    Python checking dict keys

HotTag

Archive