Remove \xa0 from a string in Python | bobbyhadz
文章推薦指數: 80 %
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..................................................................................................................................................................
延伸文章資訊
- 1Remove \xa0 from a string in Python | bobbyhadz
The \xa0 character represents non-breaking space, so the way to remove it from a string is to rep...
- 2XA0 - Prose Field Airport - AirNav
FAA Identifier: XA0. Lat/Long: 33-08-53.4000N 097-16-48.1000W 33-08.890000N 097-16.801667W 33.148...
- 3Simple Specials: -XA0 to XA30: Change of Rod End Shape
-XA0 to XA30: Change of Rod End Shape. These changes are dealt with Simple Specials System. Refer...
- 4在Python 中從字串中刪除xa0 的方法| D棧 - Delft Stack
pythonCopy import unicodedata str_hard_space='17\xa0kg on 23rd\xa0June 2021' print (str_hard_spac...
- 5python中去掉字符串中的\xa0、\t、\n - CSDN博客
\xa0 是不间断空白符 我们通常所用的空格是\x20 ,是在标准ASCII可见字符0x20~0x7e 范围内。 而\xa0 属于latin1 ( ...