OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Convert a complex string representation of a list (containing tuples, which look like function calls) into a list

  • Thread starter Thread starter Ted M.
  • Start date Start date
T

Ted M.

Guest
Here is the string representation of the list I'm trying to convert to a list:

Code:
[(3479865,
  PaginatedManagedEntityHeaders(
  success=True, 
  count=0, 
  rows=[
  ManagedEntityHeader(
  case_role='Reference',
  display_name='Person A',
  entity_type='PERSON', 
  unique_id=247878382, 
  is_active='1', 
  date_created=datetime.datetime(2021, 10, 18, 16, 29, 6, 535000, tzinfo=TzInfo(UTC))),
  ManagedEntityHeader(
  case_role='Reference', 
  display_name='Person B', 
  entity_type='PERSON', 
  unique_id=247563788, 
  is_active='0', 
  date_created=datetime.datetime(2021, 9, 8, 21, 4, 29, 631000, tzinfo=TzInfo(UTC)))]
))]

I have saved this as a JSON file: "test_fixture.json", and here is what I have tried so far:

Code:
import os
import json
import ast
import re

the_file = os.path.join(r"test_fixture.json")

file_str = open(the_file).read()

meth1 = file_str.strip("][").split(", ")
# receive a list of 25 instead of a list of 1

meth2 = ast.literal_eval(file_str)
# ValueError: malformed node or string on line 2: <ast.Call object at 0x000002241687F8E0>

meth3 = json.loads(file_str)
# JSONDecodeError: Expecting value

elements = re.findall(r"\d+", file_str)
meth4 = [int(x) for x in elements]
# receive a list of 20 instead of a list of 1

meth5 = eval(file_str)
# NameError: name 'PaginatedManagedEntityHeaders' is not defined

meth6 = list(map(int, file_str[1:-1].split(",")))
# ValueError: invalid literal for int() with base 10: '(3479865'

Is there another method that will successfully convert the string into a list of 1 element?
<p>Here is the string representation of the list I'm trying to convert to a list:</p>
<pre><code>[(3479865,
PaginatedManagedEntityHeaders(
success=True,
count=0,
rows=[
ManagedEntityHeader(
case_role='Reference',
display_name='Person A',
entity_type='PERSON',
unique_id=247878382,
is_active='1',
date_created=datetime.datetime(2021, 10, 18, 16, 29, 6, 535000, tzinfo=TzInfo(UTC))),
ManagedEntityHeader(
case_role='Reference',
display_name='Person B',
entity_type='PERSON',
unique_id=247563788,
is_active='0',
date_created=datetime.datetime(2021, 9, 8, 21, 4, 29, 631000, tzinfo=TzInfo(UTC)))]
))]
</code></pre>
<p>I have saved this as a JSON file: "test_fixture.json", and here is what I have tried so far:</p>
<pre><code>import os
import json
import ast
import re

the_file = os.path.join(r"test_fixture.json")

file_str = open(the_file).read()

meth1 = file_str.strip("][").split(", ")
# receive a list of 25 instead of a list of 1

meth2 = ast.literal_eval(file_str)
# ValueError: malformed node or string on line 2: <ast.Call object at 0x000002241687F8E0>

meth3 = json.loads(file_str)
# JSONDecodeError: Expecting value

elements = re.findall(r"\d+", file_str)
meth4 = [int(x) for x in elements]
# receive a list of 20 instead of a list of 1

meth5 = eval(file_str)
# NameError: name 'PaginatedManagedEntityHeaders' is not defined

meth6 = list(map(int, file_str[1:-1].split(",")))
# ValueError: invalid literal for int() with base 10: '(3479865'
</code></pre>
<p>Is there another method that will successfully convert the string into a list of 1 element?</p>
 

Latest posts

H
Replies
0
Views
1
haifisch123
H
A
Replies
0
Views
1
Adrian-Mihai Enache
A
H
Replies
0
Views
1
Hür Doğan ÜNLÜ
H
Top