Incrementally save changes to csv file

I’m working on a Streamlit app with the following logic:

  1. Load data from csv file.
  2. Repeat the following steps:
    a. Display several alternatives, each corresponding to a separate button.
    b. Select alternative and store information in a csv file.

For each button, the logic is something like this:


with col1:
        st.success(dialogue_original.loc[line_num]["Text line"])
        if st.button("Option 1", on_click=left_callback):
            text_feats['Number of inspections'][line_num] += 1
            text_feats[username][line_num] = 1
            open('Text_lines_feats_1.csv', 'w').write(text_feats.to_csv())
            print('Left key', line_num)

If I use this approach, every time text_feats is written to file it only stores the choices made for the last round, and the choices made before are canceled. Instead, I would like to incrementally store information, preserving the choices that were made before.

In your opinion, what is the easiest solution to implement? If possible, I would prefer to continue working with csv files instead that switching to something like MongoDB. Thanks!

Hi @albusdemens , refer 2nd last line of your code - at first glance, you will need change your mode of opening the file. For more information, refer: Python File Open

Cheers

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.