2

I have this kind of array:

'[[00487-9904-01, 00487-9904-25], [00487-9901-30]]'

How can I convert it to array of strings

[["00487-9904-01", "00487-9904-25"], ["00487-9901-30"]]

I tried ast.literal_eval and json.loads, because inside the array, 00487-9904-01 is neither a string or a number, neither of these two methods works.

3
  • 2
    Where are you getting this data from? Why is it not a JSON string to begin with?
    – gen_Eric
    Commented Oct 28, 2021 at 18:14
  • Put some quotes around the values and then do the ast.literal_eval
    – rdas
    Commented Oct 28, 2021 at 18:15
  • @rdas Wouldn't you need to parse it to quote them? I'm assuming OP doesn't control the input data.
    – wjandrea
    Commented Oct 28, 2021 at 18:54

1 Answer 1

5

You could use the yaml module:

import yaml

s = '[[00487-9904-01, 00487-9904-25], [00487-9901-30]]'
yaml.safe_load(s)

output: [['00487-9904-01', '00487-9904-25'], ['00487-9901-30']]

NB. ast.literal_eval wouldn't work as the strings are not quoted

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.