Home > Net >  How can I remove curly brackets as well as the text inside of it?
How can I remove curly brackets as well as the text inside of it?

Time:07-22

hello_world{552}.txt
{hix89}abcdefg{47181x00}.exe

How can I output these strings to look like "hello_world.txt,abcdefg.exe" with double quotation and comma without spacing?

CodePudding user response:

You can use regex and find solution:

output=[]
test=['hello_world{552}.txt','ggfj{hix89}abcdefg{47181x00}.exe']
for x in test:
    out = re.sub('{\w*}','',x)
    output.append(out)
print(output)
final_o = ",".join(output)
print(str(final_o))

Output:

hello_world.txt,ggfjabcdefg.exe

CodePudding user response:

if I understood your problem correctly, you need to get rid of all brackets and connect two strings together, plus surround with double quotes.

ls = ['hello_world{552}.txt', '{hix89}abcdefg{47181x00}.exe']

def delete_content_of_brackets(ls):
    result = ""
    bracket_is_open = False
    for element in ls:
        for character in element:
            if character == "}":
                bracket_is_open = False
            elif character == "{" or bracket_is_open:
                bracket_is_open = True
            else:
                result  = character
        result  = ","
    
    return "\""   result[:-1] "\""
  • Related