Good day!

I work with Python3 , Windows

The following situation arose: it is required to write data in the form of lists and dictionaries to a file, and then read them back in unchanged form.

Of course, it is possible to get confused and write string parsing using the re module, but this method is not universal.

Actually the question is: are there standard modules / methods for the implementation of this task?

    1 answer 1

    Use the standard pickle module. It makes it easy to write to the file and back up most of the standard python objects from the file.

    Example of the module:

     >>> import pickle >>> data = { ... 'a': [1, 2.0, 3, 4+6j], ... 'b': ("character string", b"byte string"), ... 'c': {None, True, False} ... } >>> # Сохраняем словарь data в файл data.pickle >>> with open('data.pickle', 'wb') as f: ... pickle.dump(data, f) ... # Читаем содержимое файла data.pickle в переменную data_new >>> with open('data.pickle', 'rb') as f: ... data_new = pickle.load(f) ... # Убеждаемся, что в переменной data_new лежит словарь, # идентичный первоначальному >>> print(data_new) {'c': {False, True, None}, 'a': [1, 2.0, 3, (4+6j)], 'b': ('character string', b'byte string')} # Profit! 

    You can read more about pickle, for example, here: https://pythonworld.ru/moduli/modul-pickle.html