格式化带有特殊字符的字符串
Format string with special character
我有一些带有特殊字符的字符串,我想删除这些字符。
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=}')
它可以工作,但是当我有像 「Future Core」...
这样的特殊字符时就会中断
所以我尝试了其他方式:
string = '「Future Core」[lapix] Carry Me Away (Extended Mix)'
print(''.join(i for i in string if i.isalnum()))
并且有效,但现在有一个连接的字符串,我想要类似 Future Core lapix Carry Me Away Extended Mix
的东西,像第一次尝试一样有空格。
有人可以帮助我吗? ;-
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
已解决的代码
string = '「Future Core」[lapix] Carry Me Away (Extended Mix)'
print(''.join(i for i in string if i.isalnum() or i.isspace()))
或
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))
我有一些带有特殊字符的字符串,我想删除这些字符。
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=}')
它可以工作,但是当我有像 「Future Core」...
这样的特殊字符时就会中断
所以我尝试了其他方式:
string = '「Future Core」[lapix] Carry Me Away (Extended Mix)'
print(''.join(i for i in string if i.isalnum()))
并且有效,但现在有一个连接的字符串,我想要类似 Future Core lapix Carry Me Away Extended Mix
的东西,像第一次尝试一样有空格。
有人可以帮助我吗? ;-
I need these returns from code:
Sabai With Løve Nevve Falling For You Official Music Video
andFuture Core lapix Carry Me Away Extended Mix
已解决的代码
string = '「Future Core」[lapix] Carry Me Away (Extended Mix)'
print(''.join(i for i in string if i.isalnum() or i.isspace()))
或
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))