-1

I have message which is a list and JSON in it. I managed to get JSON with raw decode. The problem is

string = "["a,b","b,c"]"

How can I convert that string into a list?

pigrammer
  • 2,603
  • 1
  • 11
  • 24
  • 4
    This is not a valid python string. – Mechanic Pig Sep 14 '22 at 11:05
  • 1
    `json.decode` it again? For the record, that's not the string you have (the lack of escaped quotes/failure to use different quotes for inner/outer quotes means it's syntactically invalid as a string literal). – ShadowRanger Sep 14 '22 at 11:05

2 Answers2

0

Use ast.literal_eval:

import ast

print(ast.literal_eval(string))

Since this is JSON, use the json module:

import json

print(json.loads(string))
pigrammer
  • 2,603
  • 1
  • 11
  • 24
0
string = "['a,b','b,c']"

print(eval(string))