Search code examples
pythonstringslicestrip

How to remove brackets '[ ]'from string using python?


I am unable to remove brackets '[ ]' from the below string.

I am getting this response through an API:

[["b", "m", "l", "a", "p", "c"], [["20,5,93767,TEST,Watch,16"], ["19,5,767, TEST,Lamb,23"], ["19,5,3767,TEST,DB,99"]]]

I have to change this response to:

"b", "m", "l", "a", "p", "c", "20,5,93767,TEST,Watch,16", "19,5,767, TEST,Lamb,23", "19,5,3767,TEST,DB,99"

I have to use python

I am using this code to remove it:

(str(content_line)[1:-1])

now I am getting this output:

"\"b', 'm', 'l', 'a', 'p', 'c\",'\"\\'20,5,93767,TEST,Watch,16\\'\", \"\\'19,5,767, TEST,Lamb,23, \"\\'19,5,3767,TEST,DB,99\\'\"'"

Solution

  • def flatten(content_line):
        result = []
        for element in content_line:
            if isinstance(element, list):
                result.extend(flatten(element))
            else:
                result.append(element)
        return result
    
    flatten(content_line)