How to properly print a list of unicode characters in python?

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

I can actually find it. So I have made a csv file with all the emoticons I want defined by their unicode. The CSV looks like this: \U0001F600. Home Public Questions Tags Users Collectives ExploreCollectives FindaJob Jobs Companies Teams StackOverflowforTeams –Collaborateandshareknowledgewithaprivategroup. CreateafreeTeam WhatisTeams? Teams CreatefreeTeam CollectivesonStackOverflow Findcentralized,trustedcontentandcollaboratearoundthetechnologiesyouusemost. Learnmore Teams Q&Aforwork Connectandshareknowledgewithinasinglelocationthatisstructuredandeasytosearch. Learnmore Howtoproperlyprintalistofunicodecharactersinpython? AskQuestion Asked 4yearsago Active 1year,3monthsago Viewed 3ktimes 5 Iamtryingtosearchforemoticonsinpythonstrings. SoIhave,forexample, em_test=['\U0001f680'] print(em_test) ['🚀'] test='Thisisateststring💰💰🚀' ifany(xintestforxinem_test): print("yes,theemoticonisthere") else: print("no,theemoticonisnotthere") yes,theemoticonisthere andifasearchem_testin 'Thisisateststring💰💰🚀' Icanactuallyfindit. SoIhavemadeacsvfilewithalltheemoticonsIwantdefinedbytheirunicode. TheCSVlookslikethis: \U0001F600 \U0001F601 \U0001F602 \U0001F923 andwhenIimportitandprintitIactullaydonotgettheemoticonsbutratherjustthetextrepresentation: ['\\U0001F600', '\\U0001F601', '\\U0001F602', '\\U0001F923', ... ] andhenceIcannotusethistosearchfortheseemoticonsinanotherstring... Isomehowknowthatthedoublebackslash\isonlyrepresentationofasingleslashbutsomehowtheunicodereaderdoesnotgetit...IdonotknowwhatI'mmissing. Anysuggestions? pythonstringunicodeemoticons Share Improvethisquestion Follow editedAug2'20at13:30 snakecharmerb 32.3k1010goldbadges5959silverbadges100100bronzebadges askedNov13'17at11:55 BullzeyeBullzeye 15111silverbadge1111bronzebadges 1 areyousureyoudon'thaveanylinefeeds? – Ma0 Nov13'17at12:03 Addacomment  |  2Answers 2 Active Oldest Votes 3 YoucandecodethoseUnicodeescapesequenceswith.decode('unicode-escape').However,.decodeisabytesmethod,soifthosesequencesaretextratherthanbytesyoufirstneedtoencodethemintobytes.Alternatively,youcan(probably)openyourCSVfileinbinarymodeinordertoreadthosesequencesasbytesratherthanastextstrings. Justforfun,I'llalsouseunicodedatatogetthenamesofthoseemojis. importunicodedataasud emojis=[ '\\U0001F600', '\\U0001F601', '\\U0001F602', '\\U0001F923', ] foruinemojis: s=u.encode('ASCII').decode('unicode-escape') print(u,ud.name(s),s) output \U0001F600GRINNINGFACE😀 \U0001F601GRINNINGFACEWITHSMILINGEYES😁 \U0001F602FACEWITHTEARSOFJOY😂 \U0001F923ROLLINGONTHEFLOORLAUGHING🤣 Thisshouldbemuchfasterthanusingast.literal_eval.Andifyoureadthedatainbinarymodeitwillbeevenfastersinceitavoidstheinitialdecodingstepwhilereadingthefile,aswellasallowingyoutoeliminatethe.encode('ASCII')call. Youcanmakethedecodingalittlemorerobustbyusing u.encode('Latin1').decode('unicode-escape') butthatshouldn'tbenecessaryforyouremojidata.AndasIsaidearlier,itwouldbeevenbetterifyouopenthefileinbinarymodetoavoidtheneedtoencodeit. Share Improvethisanswer Follow editedNov13'17at14:59 answeredNov13'17at12:32 PM2RingPM2Ring 51.1k55goldbadges6767silverbadges156156bronzebadges 11 Thisisgreatandworks.Ionlyhaveanissuewithsomespecificemoticons:'\U00023EB'->getanerrorofSyntaxError:(unicodeerror)'unicodeescape'codeccan'tdecodebytesinposition0-8:truncated\UXXXXXXXXescape – Bullzeye Nov13'17at12:56 @BullzeyePythonconsiders'\U00023EB'tobeinvalid:"BigU"Unicodeescapesmustcontain8hexdigits.Wecouldhandlethatinmycode,butit'sprobablybettertofixitinthecodethatbuildstheCSV. – PM2Ring Nov13'17at12:58 Yes,justwonderingwherethenthefollowingcodeU+23EBtakenfromthesitecamefromunicode.org/emoji/charts/full-emoji-list.html – Bullzeye Nov13'17at12:59 Mybad,justseemstoneedonemore0insteadofthe+signsothatitaddsupto8.Sorry – Bullzeye Nov13'17at13:04 @BullzeyeAlternatively,forsmallercodepointsthatfitin4hexdigitsyoucanusethe"smallu"escapecode:'\u23eb'.'unicode-escape'canhandlethat.Italsohandlesstufflike'\\x41'AndofcourseitalsohandlesplainASCIItext. – PM2Ring Nov13'17at13:06  |  Show6morecomments 1 1.keepingyourcsvas-is: it'sabloatedsolution,butusingast.literal_evalworks: importast s='\\U0001F600' x=ast.literal_eval('"{}"'.format(s)) print(hex(ord(x))) print(x) Iget0x1f600(whichiscorrectcharcode)andsomeemoticoncharacter(😀).(wellIhadtocopy/pasteastrangecharfrommyconsoletothisanswertextfieldbutthat'saconsoleissuebymyend,otherwisethatworks) justsurroundwithquotestoallowasttotaketheinputasstring. 2.usingcharactercodesdirectly maybeyou'dbebetteroffbystoringthecharactercodesthemselvesinsteadofthe\Uformat: print(chr(0x1F600)) doesexactlythesame(soastisslightlyoverkill) yourcsvcouldcontain: 0x1F600 0x1F601 0x1F602 0x1F923 thenchr(int(row[0],16))woulddothetrickwhenreadingit:exampleifone1rowinCSV(orfirstrow) withopen("codes.csv")asf: cr=csv.reader(f) codes=[int(row[0],16)forrowincr] Share Improvethisanswer Follow editedJun20'20at9:12 CommunityBot 111silverbadge answeredNov13'17at12:00 Jean-FrançoisFabre♦Jean-FrançoisFabre 129k2222goldbadges113113silverbadges183183bronzebadges 1 ok-theprintworksgreat!CanIkindlyaskyoutoelaborateonhowtoreadthecsv-donotgetthechr(int(row[0],16))part-howisthisintegratedforexampleinpos_emo_twitter=pandas.read_csv('listposemoticons.csv') – Bullzeye Nov13'17at12:57 Addacomment  |  YourAnswer ThanksforcontributingananswertoStackOverflow!Pleasebesuretoanswerthequestion.Providedetailsandshareyourresearch!Butavoid…Askingforhelp,clarification,orrespondingtootheranswers.Makingstatementsbasedonopinion;backthemupwithreferencesorpersonalexperience.Tolearnmore,seeourtipsonwritinggreatanswers. Draftsaved Draftdiscarded Signuporlogin SignupusingGoogle SignupusingFacebook SignupusingEmailandPassword Submit Postasaguest Name Email Required,butnevershown PostYourAnswer Discard Byclicking“PostYourAnswer”,youagreetoourtermsofservice,privacypolicyandcookiepolicy Nottheansweryou'relookingfor?Browseotherquestionstaggedpythonstringunicodeemoticonsoraskyourownquestion. TheOverflowBlog Podcast395:Whoisbuildingcloudsfortheindependentdeveloper? Explodingturkeysandhownottothawyourfrozenbird:Topturkeyquestions... FeaturedonMeta Nowlive:Afullyresponsiveprofile Reducingtheweightofourfooter TwoBornottwoB-Farewell,BoltClockandBhargav! Linked 1 Addasinglebackslash("\")tostringinpython Related 3231 HowdoIcheckifalistisempty? 5525 Howtoexecuteaprogramorcallasystemcommand? 4999 HowcanIsafelycreateanesteddirectoryinPython? 2432 HowdoIgetasubstringofastringinPython? 4352 Howtomakeaflatlistoutofalistoflists 3466 HowdoIlistallfilesofadirectory? 1965 HowcanIprintliteralcurly-bracecharactersinastringandalsouse.formatonit? 1092 Whatdoesthe'b'characterdoinfrontofastringliteral? 2352 HowdoIlowercaseastringinPython? 1410 UnicodeEncodeError:'ascii'codeccan'tencodecharacteru'\xa0'inposition20:ordinalnotinrange(128) HotNetworkQuestions Whatdoes"Thebargaintotheletter"mean? Whymaximumaposterior,notmaximumposterior? WhydidAtarifloppiesrunat288RPM? HowtojustifysmallHomininsnotbeingconqueredby(anatomicallymodern)Humans? Howtoaskfriendnottoleavesoquickly,oratleastletmeknowifhedoesn'tplanonstayingverylong? Doesthe1xoscilloscopeprobesettingslowdownMhzdigitalsignals? Harmonicdivisornumbers WhyarethehiddenitemsinFireRed/LeafGreensometimesabsent? TravellingtoSpainonanIrishPassport(IamaUKandIrishCitizen-mywifeisUKonly) HowcanInotgetunfairlyblamedforthingsbymyboss? Whydoweneedinsulationmaterialbetweentwowalls? Meaningof"ruinsomethingforsomeone" Whyarecerealgrainssoimportanttoagricultureandcivilization? Sci-fistorywherepeoplearereincarnatedathubsandamanwantstofigureoutwhatishappening Mini-dashasageneralbonusaction:whatvaluewouldbebalanced? IbrokemyGstringandmyother3stringsareoutoftunetoo Whichpart(s)hasthegreatestslopeofpriceperpound(kg)? Convertprefixtoinfix WhyarelegaldecisionsintheUSsopoliticized? Whatpotentialdifferenceorcurrentwouldkillaperson? CanImakeahelixwith4strands? Whatdothescammersdowhentheygetaccesstosomeone'sonlinebanking? AreJapaneseprincessesandprincesreferredtobyadifferentwordinJapanesethanprincessesandprincesoutsideofJapan? Foraspacecraftorbitingaplanet,orbitalspeedisinverselyproportionaltoorbitradius.Butspeedmustbeincreasedtoincreaseorbitradius? morehotquestions Questionfeed SubscribetoRSS Questionfeed TosubscribetothisRSSfeed,copyandpastethisURLintoyourRSSreader. lang-py Yourprivacy Byclicking“Acceptallcookies”,youagreeStackExchangecanstorecookiesonyourdeviceanddiscloseinformationinaccordancewithourCookiePolicy. Acceptallcookies Customizesettings  



請為這篇文章評分?