简体   繁体   中英

Format string with special character

I have some string with special characters, and i want to remove these characters.

def clean_special(input: str):  # This function is my cleaner
    # Idk how change these '.replace()' to something more efficient
    cleaning = re.sub(r'[()&.-]', ' ', input).replace("’", '')
    cleaning = cleaning.replace(',', '').replace("'", '').replace("[", '').replace("]", '').strip()
    return " ".join(cleaning.split())


original_string = 'Sabai, With Løve & Nevve - Falling For You (Official Music Video)'  # Title of music, with some special characters
cleaned_string = clean_special(original_string)
print(f'{original_string=}\n{cleaned_string=}')

It works, but when i have special characters like 「Future Core」... just breaks

So i tryied other way:

string = '「Future Core」[lapix] Carry Me Away (Extended Mix)'

print(''.join(i for i in string if i.isalnum()))

And works, but now have a joined string, i wanted something like Future Core lapix Carry Me Away Extended Mix with spaces like the first attempt.

Someone can help me? ;-

I need these returns from code: Sabai With Løve Nevve Falling For You Official Music Video and Future Core lapix Carry Me Away Extended Mix

Solved code

string = '「Future Core」[lapix] Carry Me Away (Extended Mix)'

print(''.join(i for i in string if i.isalnum() or i.isspace()))

or

def clean_special(input: str):
    cleaning = re.sub(r'[^\w\s]+', '', input)
    return " ".join(cleaning.split())


string = '「Future Core」[lapix] Carry Me Away (Extended Mix)'
print(clean_special(string))

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