Search code examples
pythondictionaryglobalpersist

Store dynamic values in a data structure


I have some text which is stored in an array 'A' (global data structure) whenever I run my program.

Now, the next time I run the same program, I want to append new text to the same array 'A'.

Eg: 1st time I run the program, the input values (1,12,32) are added into the array A, next time I run the same program with input values (18,7,92) .. I want the resulting array to be [1,12,32,18,7,92]

So essentially each time I run the program, new input data should be appended to that global Array.

How can I achieve this ?

Thank You.


Solution

  • Try to use pickle module:

    import pickle
    data = [int(el) for el in raw_input().split()]
    try:
        new = pickle.load(open("save.p", "rb")) + data
    except IOError:
        new = data
    pickle.dump(new, open("save.p", "wb"))
    print pickle.load(open("save.p", "rb"))