K
K
Kostyan4ik2021-05-25 17:47:18
Python
Kostyan4ik, 2021-05-25 17:47:18

How to parse lists in a string?

Good afternoon. I need to parse a string containing lists into lists. Here is an example line content:

[
[1, "Иван Иванович Иванов", "Иванов И.И."],
 [2, "Макар Макарович Макаров", "Макароы М.М."]
]

Of course, this can be done with regular expressions, but perhaps there are other, more humane ways. Please let me know if there are other ways to do this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Kuts, 2021-05-25
@Kostyan4ik

If I understand correctly, here is a more humane way:

import ast

data = '''[
  [1, "Иван Иванович Иванов", "Иванов И.И."],
  [2, "Макар Макарович Макаров", "Макароы М.М."]
 ]'''
res = ast.literal_eval(data)
# 
type(res)
# <class 'list'>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question