List in mongodb (formated as string ) convert to list st.session state

Hi There.
i using mongodb for my database and use jinja2 to display my template

But i have problem. when i write like this, i can get my data correctly.

if 'data' not in st.session_state:
    st.session_state['data'] = []

qt_list2=[1, '4455abc'], [2, '5451abc']
    for line in qt_list2:
        st.session_state['data'].append(line)
st.write(st.session_state['data'])

and then the result will be like that :

[[1, '4455abc'], [2, '5451abc']]

i save this list [1, β€˜4455abc’], [2, β€˜5451abc’] to mongodb and the i call again the result like this

"[[1, '4455abc'], [2, '5451abc']]"

how possible can i remove double quote (") in streamlit or python. so streamlit can understand if that is a list not a string.

Hello there,
To convert the string representation of a list, such as β€œ[[1, β€˜4455abc’], [2, β€˜5451abc’]]”, back into a list object in Python, you can use the ast.literal_eval() function from the ast module.

import ast

string_data = β€œ[[1, β€˜4455abc’], [2, β€˜5451abc’]]”
list_data = ast.literal_eval(string_data)

print(list_data)
import ast

string_data = β€œ[[1, β€˜4455abc’], [2, β€˜5451abc’]]”
list_data = ast.literal_eval(string_data)

print(list_data)

In this code, the ast.literal_eval() function safely evaluates the string as a Python literal, which in this case is a list.

import ast
import streamlit as st

string_data = β€œ[[1, β€˜4455abc’], [2, β€˜5451abc’]]”
list_data = ast.literal_eval(string_data)

st.write(list_data)

Thanks, that code work well

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