简体   繁体   中英

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\\'\"'"
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)

You can use the str.join for this

print(', '.join(content_line))

# input string a_st = """ [["b", "m", "l", "a", "p", "c"], [["20,5,93767,TEST,Watch,16"], ["19,5,767, TEST,Lamb,23"], ["19,5,3767,TEST,DB,99"]]] """ # replace brackets '[]' with '' output = a_st.replace('[','').replace(']','') # output print(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"'

I solved this by using String instead of tuples.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM