Home > OS >  Format string with special character
Format string with special character

Time:12-18

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

CodePudding user response:

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))
  • Related