Remove \xa0 from a string in Python | bobbyhadz

文章推薦指數: 80 %
投票人數:10人

The \xa0 character represents non-breaking space, so the way to remove it from a string is to replace it with a space. ☰HomeBookAboutContactsHomeBookAboutContactsGitHubLinkedinTwitterRemove\xa0fromastringinPythonBorislavHadzhievLastupdated:Jul10,2022PhotofromUnsplashRemove\xa0fromastringinPython#Usetheunicodedata.normalize()methodtoremove\xa0fromastring,e.g. result=unicodedata.normalize('NFKD',my_str).Theunicodedata.normalize methodreturnsthenormalformfortheprovidedunicodestringbyreplacingall compatibilitycharacterswiththeirequivalents.main.pyCopied!importunicodedata my_str='hello\xa0world' #✅remove\xa0fromstringusingunicodedata.normalize() result=unicodedata.normalize('NFKD',my_str) print(result)#👉️'helloworld' #---------------------------------------- #✅remove\xa0fromstringusingstr.replace() result=my_str.replace('\xa0','') print(result)#👉️'helloworld' #---------------------------------------- #✅remove\xa0fromlistofstrings my_list=['hello\xa0','\xa0world'] result=[string.replace('\xa0','')forstringinmy_list] print(result)#👉️['hello','world'] The\xa0characterrepresentsnon-breakingspace,sothewaytoremoveitfromastringistoreplaceitwithaspace.The unicodedata.normalize methodreturnsthenormalformfortheprovidedUnicodestring.Thefirstargumentistheform-NFKDinourcase.ThenormalformNFDK replacesallcompatibilitycharacterswiththeirequivalents.Sincetheequivalentofthe\xa0characterisaspace,itgetsreplacedbyaspace.IfyougetunexpectedresultswhenusingtheNFKDform,tryusingoneof NFC,NFKCandNFD.TheNFKCformfirstappliescompatibilitydecomposition,thencanonical decomposition.main.pyCopied!importunicodedata my_str='hello\xa0world' result=unicodedata.normalize('NFKC',my_str) print(result)#👉️'helloworld' Alternatively,youcanusethestr.replace()method.Usethestr.replace()methodtoremove\xa0fromastring,e.g. result=my_str.replace('\xa0','').Thestr.replace()methodwillreplace alloccurrencesofthe\xa0(non-breakingspace)characterwithaspace.main.pyCopied!my_str='hello\xa0world' result=my_str.replace('\xa0','') print(result)#👉️'helloworld' Sincethe\xa0characterrepresentsanon-breakingspace,wecansimply replaceitwithaspace.Thestr.replace methodreturnsacopyofthestringwithalloccurrencesofasubstringreplaced bytheprovidedreplacement.Themethodtakesthefollowingparameters:NameDescriptionoldThesubstringwewanttoreplaceinthestringnewThereplacementforeachoccurrenceofoldcountOnlythefirstcountoccurrencesarereplaced(optional)Notethatthemethoddoesn'tchangetheoriginalstring.Stringsareimmutable inPython.Remove\xa0fromalistofstringsinPython#Toremove\xa0charactersfromalistofstrings:Usealistcomprehensiontoiterateoverthelist.Oneachiteration,usethestr.replace()methodtoreplaceoccurrencesof \xa0withaspace.Thestringsinthenewlistwon'tcontainany\xa0characters.main.pyCopied!my_list=['hello\xa0','\xa0world'] result=[string.replace('\xa0','')forstringinmy_list] print(result)#👉️['hello','world'] Weusedalistcomprehensiontoiterateoverthelist.Listcomprehensionsareusedtoperformsomeoperationforeveryelement,orselectasubsetofelementsthatmeetacondition.Oneachiteration,wereplaceoccurrencesofthe\xa0characterwithaspace andreturntheresult.IwroteabookinwhichIshareeverythingIknowabouthowtobecomeabetter,moreefficientprogrammer.YoucanusethesearchfieldonmyHomePagetofilterthroughallofmyarticles.ShareShareShareShareShareBorislavHadzhievWebDeveloperTwitterGitHubLinkedinSUPPORTME:)AboutContactsPolicyTerms&ConditionsTwitterGitHubLinkedinCopyright©2022BorislavHadzhievSearchforposts0..................................................................................................................................................................



請為這篇文章評分?