-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?

Kanchon Gharami
  • 777
  • 1
  • 11
  • 30

1 Answers1

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)
Vincent Bénet
  • 1,212
  • 6
  • 20