Home > Software design >  How to convert string of array to array of string?
How to convert string of array to array of string?

Time:10-29

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.

CodePudding user response:

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

  • Related