-1

I have a string in my python like:

str = "[3705049, 3705078, 3705082, 3705086, 3705093, 3705096]"

Now I need to convert it to an array or list like:

arr = [3705049, 3705078, 3705082, 3705086, 3705093, 3705096]

I have tried like this:

str = "[3705049, 3705078, 3705082, 3705086, 3705093, 3705096]"
arr = list(str)
print(arr)

But it provides output like this:

['[', '3', '7', '0', '5', '0', '4', '9', ',', ' ', '3', '7', '0', '5', '0', '7', '8', ',', ' ', '3', '7', '0', '5', '0', '8', '2', ',', ' ', '3', '7', '0', '5', '0', '8', '6', ',', ' ', '3', '7', '0', '5', '0', '9', '3', ',', ' ', '3', '7', '0', '5', '0', '9', '6', ']']

Please suggest how can I fix this?

2
  • 3
    ast.literal_eval(str) Commented Oct 17, 2021 at 16:34
  • 1
    you need to import ast Commented Oct 17, 2021 at 16:36

1 Answer 1

1

You can use json.loads to load your datas:

import json
string = "[3705049, 3705078, 3705082, 3705086, 3705093, 3705096]"

arr = [3705049, 3705078, 3705082, 3705086, 3705093, 3705096]

print(json.loads(string) == arr)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.