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..................................................................................................................................................................
延伸文章資訊
- 1RX8804CE XA0 Epson Timing - Mouser Electronics
RX8804CE XA0 Epson Timing Epson Electronics America RX8804CE:XA0 REAL TIME CLOCK +/-3.4PPM DTCXO ...
- 2在Python 中從字串中刪除xa0 的方法| D棧 - Delft Stack
pythonCopy import unicodedata str_hard_space='17\xa0kg on 23rd\xa0June 2021' print (str_hard_spac...
- 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...
- 4Day28 Python 基礎- 字符轉編碼操作_V2 - iT 邦幫忙
... b'\xe4\xbd\xa0\xe5\xa5\xbd\xe6\x8a\xbc' Process finished with exit code 0. 唔…上面代碼第三個打印出來真的是 u...
- 5How to remove \xa0 from string in Python? - Stack Overflow
\xa0 is actually non-breaking space in Latin1 (ISO 8859-1), also chr(160). You should replace it ...