Search code examples
pythonpandasdataframetext-filesbinaryfiles

How to edit data in a text file using python?


I am fairly new to Python(and handling files) , I am using pandas and storing a dataframe in a text file.

My program requires constant changes in the dataframe , which in turn requires to be updated in the text.

Writing the whole dataframe over and over again ,would not be efficient(i guess,given that i may want to update only a cell)! Appending data would mean adding the whole dataframe again(which is not what i want).

And , then there is Binary file , should i store as that , open it ,edit as normal python object and it reflects back in the file?

How do i achieve this?


Solution

  • Besides the discussion of whether or not you should use the database, it seems what you need is a quick way to save/read again the DataFrame.

    You can do it with pickle. “Pickling” is the process whereby a Python object hierarchy is converted into a byte stream, and “unpickling” is the inverse operation, whereby a byte stream (from a binary file or bytes-like object) is converted back into an object hierarchy.

    import pickle
    
    # Save the DataFrame
    pickle.dump(df, open( "dataFrame.p", "wb" ))
    
    # Load the DataFrame
    df_read =pickle.load( open( "dataFrame.p", "rb"))