Answer the question
In order to leave comments, you need to log in
How to restore a list from a string?
Hello! Let's say I have a list (weighing about 15Mb). I, like a string , push it into the database, and then extract it. The output is a string. How can I restore the list from this line (do I need to somehow just change the type of the object)? The problem is that there are side commas in the list and split is useless.
Ps The option is to change the data type of the database column, but all the lists (list data types) that I know do not contain so much.
Answer the question
In order to leave comments, you need to log in
Is the list in this format? a = '[1,2,3]'
Weakly imagine 15MB of text in one line, this should be a huge line. But anyway,
from ast import literal_eval
a = literal_eval('[1,2,3]')
import json
a = json.loads(a)
You can also serialize\deserialize this list to \from json.
huge_string = json.dumps(huge_list)
restored_list = json.loads(huge_string)
What is your list? If the lines of the list are separated by a line break, then writing and reading preserve the line break and there should be no problems.
And if you have a list of strings that may contain hyphens, then you need to store this either through serialization, or like normal people in a separate table.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question