M
M
Maxim Belozerov2021-08-04 09:39:45
Python
Maxim Belozerov, 2021-08-04 09:39:45

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

3 answer(s)
S
soremix, 2021-08-04
@Python_py

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]')

upd: better json.loads() as mentioned below, flew out of my head
import json
a = json.loads(a)

S
Sergey Pankov, 2021-08-04
@trapwalker

You can also serialize\deserialize this list to \from json.

huge_string = json.dumps(huge_list)
restored_list = json.loads(huge_string)

H
HemulGM, 2021-08-04
@HemulGM

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 question

Ask a Question

731 491 924 answers to any question