Unicode (UTF-8) reading and writing to files in Python
文章推薦指數: 80 %
This works for reading a file with UTF-8 encoding in Python 3.2: import codecs f = codecs.open('file_name.txt', 'r', 'UTF-8') for line in f: ... Home Public Questions Tags Users Companies Collectives ExploreCollectives Teams StackOverflowforTeams –Startcollaboratingandsharingorganizationalknowledge. CreateafreeTeam WhyTeams? Teams CreatefreeTeam Collectives™onStackOverflow Findcentralized,trustedcontentandcollaboratearoundthetechnologiesyouusemost. LearnmoreaboutCollectives Teams Q&Aforwork Connectandshareknowledgewithinasinglelocationthatisstructuredandeasytosearch. LearnmoreaboutTeams Unicode(UTF-8)readingandwritingtofilesinPython AskQuestion Asked 13years,8monthsago Modified 1monthago Viewed 967ktimes 385 I'mhavingsomebrainfailureinunderstandingreadingandwritingtexttoafile(Python2.4). #Thestring,whichhasana-acuteinit. ss=u'Capit\xe1n' ss8=ss.encode('utf8') repr(ss),repr(ss8) ("u'Capit\xe1n'","'Capit\xc3\xa1n'") printss,ss8 print>>open('f1','w'),ss8 >>>file('f1').read() 'Capit\xc3\xa1n\n' SoItypeinCapit\xc3\xa1nintomyfavoriteeditor,infilef2. Then: >>>open('f1').read() 'Capit\xc3\xa1n\n' >>>open('f2').read() 'Capit\\xc3\\xa1n\n' >>>open('f1').read().decode('utf8') u'Capit\xe1n\n' >>>open('f2').read().decode('utf8') u'Capit\\xc3\\xa1n\n' WhatamInotunderstandinghere?Clearlythereissomevitalbitofmagic(orgoodsense)thatI'mmissing.Whatdoesonetypeintotextfilestogetproperconversions? WhatI'mtrulyfailingtogrokhere,iswhatthepointoftheUTF-8representationis,ifyoucan'tactuallygetPythontorecognizeit,whenitcomesfromoutside.MaybeIshouldjustJSONdumpthestring,andusethatinstead,sincethathasanasciiablerepresentation!Moretothepoint,isthereanASCIIrepresentationofthisUnicodeobjectthatPythonwillrecognizeanddecode,whencominginfromafile?Ifso,howdoIgetit? >>>printsimplejson.dumps(ss) '"Capit\u00e1n"' >>>print>>file('f3','w'),simplejson.dumps(ss) >>>simplejson.load(open('f3')) u'Capit\xe1n' pythonunicodeutf-8io Share Improvethisquestion Follow editedJan4,2017at18:07 PeterMortensen 30.6k2121goldbadges102102silverbadges124124bronzebadges askedJan29,2009at15:01 GreggLindGreggLind 20.1k1515goldbadges6666silverbadges8181bronzebadges Addacomment | 14Answers 14 Sortedby: Resettodefault Highestscore(default) Trending(recentvotescountmore) Datemodified(newestfirst) Datecreated(oldestfirst) 838 Ratherthanmesswith.encodeand.decode,specifytheencodingwhenopeningthefile.Theiomodule,addedinPython2.6,providesanio.openfunction,whichallowsspecifyingthefile'sencoding. SupposingthefileisencodedinUTF-8,wecanuse: >>>importio >>>f=io.open("test",mode="r",encoding="utf-8") Thenf.readreturnsadecodedUnicodeobject: >>>f.read() u'Capit\xe1l\n\n' In3.x,theio.openfunctionisanaliasforthebuilt-inopenfunction,whichsupportstheencodingargument(itdoesnotin2.x). Wecanalsouseopenfromthecodecsstandardlibrarymodule: >>>importcodecs >>>f=codecs.open("test","r","utf-8") >>>f.read() u'Capit\xe1l\n\n' Note,however,thatthiscancauseproblemswhenmixingread()andreadline(). Share Improvethisanswer Follow editedSep3at22:03 KarlKnechtel 59.2k99goldbadges8686silverbadges130130bronzebadges answeredMay10,2009at0:45 TimSwastTimSwast 13.5k44goldbadges3838silverbadges5858bronzebadges 9 74 Worksperfectlyforwritingfilestoo,insteadofopen(file,'w')docodecs.open(file,'w','utf-8')solved – MattConnolly Mar4,2011at2:12 6 Doesthecodecs.open(...)methodalsofullyconformtothewithopen(...):style,wherethewithcaresaboutclosingthefileafterallisdone?Itseemstoworkanyway. – try-catch-finally Mar4,2013at18:09 2 @try-catch-finallyYes.Iusewithcodecs.open(...)asf:allthetime. – TimSwast Jul8,2013at14:27 7 IwishIcouldupvotethisahundredtimes.Afteragonizingforseveraldaysoverencodingissuescausedbyalotofmixeddataandgoingcross-eyedreadingaboutencoding,thisanswerislikewaterinadesert.WishI'dseenitsooner. – MikeGirard Jul21,2013at18:24 1 Watchoutforcodecs.Considerioinstead. – personal_cloud Sep27,2017at20:57 | Show4morecomments 119 Inthenotationu'Capit\xe1n\n'(shouldbejust'Capit\xe1n\n'in3.x,andmustbein3.0and3.1),the\xe1representsjustonecharacter.\xisanescapesequence,indicatingthate1isinhexadecimal. WritingCapit\xc3\xa1nintothefileinatexteditormeansthatitactuallycontains\xc3\xa1.Thoseare8bytesandthecodereadsthemall.Wecanseethisbydisplayingtheresult: #Python3.x-readingthefileasbytesratherthantext, #toensureweseetherawdata >>>open('f2','rb').read() b'Capit\\xc3\\xa1n\n' #Python2.x >>>open('f2').read() 'Capit\\xc3\\xa1n\n' Instead,justinputcharacterslikeáintheeditor,whichshouldthenhandletheconversiontoUTF-8andsaveit. In2.x,astringthatactuallycontainsthesebackslash-escapesequencescanbedecodedusingthestring_escapecodec: #Python2.x >>>print'Capit\\xc3\\xa1n\n'.decode('string_escape') Capitán TheresultisastrthatisencodedinUTF-8wheretheaccentedcharacterisrepresentedbythetwobytesthatwerewritten\\xc3\\xa1intheoriginalstring.Togetaunicoderesult,decodeagainwithUTF-8. In3.x,thestring_escapecodecisreplacedwithunicode_escape,anditisstrictlyenforcedthatwecanonlyencodefromastrtobytes,anddecodefrombytestostr.unicode_escapeneedstostartwithabytesinordertoprocesstheescapesequences(theotherwayaround,itaddsthem);andthenitwilltreattheresulting\xc3and\xa1ascharacterescapesratherthanbyteescapes.Asaresult,wehavetodoabitmorework: #Python3.x >>>'Capit\\xc3\\xa1n\n'.encode('ascii').decode('unicode_escape').encode('latin-1').decode('utf-8') 'Capitán\n' Share Improvethisanswer Follow editedSep3at22:29 KarlKnechtel 59.2k99goldbadges8686silverbadges130130bronzebadges answeredJan29,2009at15:11 unbeknownunbeknown 7 So,what'sthepointoftheutf-8encodedformatifpythoncanreadinfilesusingit?Inotherwords,isthereanyasciirepresentationthatpythonwillreadin\xc3as1byte? – GreggLind Jan29,2009at16:51 4 Theanswertoyour"So,what'sthepoint…"questionis"Mu."(sincePythoncanreadfilesencodedinUTF-8).Foryoursecondquestion:\xc3isnotpartoftheASCIIset.Perhapsyoumean"8-bitencoding"instead.YouareconfusedaboutUnicodeandencodings;it'sok,manyare. – tzot Jan30,2009at12:16 9 Tryreadingthisasaprimer:joelonsoftware.com/articles/Unicode.html – tzot Jan30,2009at12:16 note:u'\xe1'isoneUnicodecodepointU+00e1thatcanberepresentedusing1ormorebytesdependingoncharacterencoding(itis2bytesinutf-8).b'\xe1'isonebyte(anumber225),whatletterifanyitcanrepresentdependsoncharacterencodingusedtodecodeite.g.,itisб(U+0431)incp1251,с(U+0441)incp866,etc. – jfs Jun15,2013at6:31 13 ItisamazinghowmanyBritishcoderssay"justuseascii"andthenfailtorealisethatthe£signisnotit.Mostarenotawarethatascii!=localcodepage(ielatin1). – DannyStaple Sep5,2013at12:58 | Show2morecomments 73 NowallyouneedinPython3isopen(Filename,'r',encoding='utf-8') [Editon2016-02-10forrequestedclarification] Python3addedtheencodingparametertoitsopenfunction.Thefollowinginformationabouttheopenfunctionisgatheredfromhere:https://docs.python.org/3/library/functions.html#open open(file,mode='r',buffering=-1, encoding=None,errors=None,newline=None, closefd=True,opener=None) Encodingisthenameoftheencodingusedtodecodeorencodethe file.Thisshouldonlybeusedintextmode.Thedefaultencodingis platformdependent(whateverlocale.getpreferredencoding() returns),butanytextencodingsupportedbyPythoncanbeused. Seethecodecsmoduleforthelistofsupportedencodings. Sobyaddingencoding='utf-8'asaparametertotheopenfunction,thefilereadingandwritingisalldoneasutf8(whichisalsonowthedefaultencodingofeverythingdoneinPython.) Share Improvethisanswer Follow editedMar4,2018at8:20 Derlin 9,23122goldbadges2727silverbadges4949bronzebadges answeredFeb10,2016at16:03 DakusanDakusan 6,34544goldbadges3131silverbadges4343bronzebadges 2 Couldyoupleaseelaboratemoreyouransweraddingalittlemoredescriptionaboutthesolutionyouprovide? – abarisone Feb10,2016at16:26 3 Itlooksthisisavailableinpython2usingthecodecsmodule-codecs.open('somefile',encoding='utf-8')stackoverflow.com/a/147756/149428 – TaylorD.Edmiston Aug14,2016at1:43 Addacomment | 18 So,I'vefoundasolutionforwhatI'mlookingfor,whichis: printopen('f2').read().decode('string-escape').decode("utf-8") Therearesomeunusualcodecsthatareusefulhere.ThisparticularreadingallowsonetotakeUTF-8representationsfromwithinPython,copythemintoanASCIIfile,andhavethembereadintoUnicode.Underthe"string-escape"decode,theslasheswon'tbedoubled. ThisallowsforthesortofroundtripthatIwasimagining. Share Improvethisanswer Follow editedJan4,2017at18:37 PeterMortensen 30.6k2121goldbadges102102silverbadges124124bronzebadges answeredJan29,2009at20:01 GreggLindGreggLind 20.1k1515goldbadges6666silverbadges8181bronzebadges 0 Addacomment | 18 ThisworksforreadingafilewithUTF-8encodinginPython3.2: importcodecs f=codecs.open('file_name.txt','r','UTF-8') forlineinf: print(line) Share Improvethisanswer Follow editedSep3at21:56 KarlKnechtel 59.2k99goldbadges8686silverbadges130130bronzebadges answeredAug19,2014at8:09 SinaSina 42144silverbadges77bronzebadges 0 Addacomment | 14 #-*-encoding:utf-8-*- #convertingaunknownformattingfileinutf-8 importcodecs importcommands file_location="jumper.sub" file_encoding=commands.getoutput('file-b--mime-encoding%s'%file_location) file_stream=codecs.open(file_location,'r',file_encoding) file_output=codecs.open(file_location+"b",'w','utf-8') forlinfile_stream: file_output.write(l) file_stream.close() file_output.close() Share Improvethisanswer Follow answeredFeb8,2012at20:24 RicardoRicardo 59888silverbadges1111bronzebadges Addacomment | 8 Asidefromcodecs.open(),io.open()canbeusedinboth2.xand3.xtoreadandwritetextfiles.Example: importio text=u'á' encoding='utf8' withio.open('data.txt','w',encoding=encoding,newline='\n')asfout: fout.write(text) withio.open('data.txt','r',encoding=encoding,newline='\n')asfin: text2=fin.read() asserttext==text2 Share Improvethisanswer Follow editedSep3at22:33 KarlKnechtel 59.2k99goldbadges8686silverbadges130130bronzebadges answeredJun21,2017at9:37 RyanRyan 21533silverbadges55bronzebadges 2 1 +1ioismuchbetterthancodecs. – personal_cloud Sep27,2017at20:58 Yes,usingioisbetter;ButIwrotethewithstatementlikethiswithio.open('data.txt','w','utf-8')asfile:andgotanerror:TypeError:anintegerisrequired.AfterIchangedtowithio.open('data.txt','w',encoding='utf-8')asfile:anditworked. – EvanHu Jan2,2018at5:33 Addacomment | 6 ToreadinanUnicodestringandthensendtoHTML,Ididthis: fileline.decode("utf-8").encode('ascii','xmlcharrefreplace') Usefulforpythonpoweredhttpservers. Share Improvethisanswer Follow answeredSep18,2014at14:38 prajpraj 6111silverbadge11bronzebadge Addacomment | 6 Well,yourfavoritetexteditordoesnotrealizethat\xc3\xa1aresupposedtobecharacterliterals,butitinterpretsthemastext.That'swhyyougetthedoublebackslashesinthelastline--it'snowarealbackslash+xc3,etc.inyourfile. IfyouwanttoreadandwriteencodedfilesinPython,bestusethecodecsmodule. Pastingtextbetweentheterminalandapplicationsisdifficult,becauseyoudon'tknowwhichprogramwillinterpretyourtextusingwhichencoding.Youcouldtrythefollowing: >>>s=file("f1").read() >>>printunicode(s,"Latin-1") Capitán ThenpastethisstringintoyoureditorandmakesurethatitstoresitusingLatin-1.Undertheassumptionthattheclipboarddoesnotgarblethestring,theroundtripshouldwork. Share Improvethisanswer Follow editedJan4,2017at18:11 PeterMortensen 30.6k2121goldbadges102102silverbadges124124bronzebadges answeredJan29,2009at15:13 TorstenMarekTorstenMarek 80.8k2121goldbadges9191silverbadges9898bronzebadges Addacomment | 6 Youhavestumbledoverthegeneralproblemwithencodings:HowcanItellinwhichencodingafileis? Answer:Youcan'tunlessthefileformatprovidesforthis.XML,forexample,beginswith: Thisheaderwascarefullychosensothatitcanbereadnomattertheencoding.Inyourcase,thereisnosuchhint,henceneitheryoureditornorPythonhasanyideawhatisgoingon.Therefore,youmustusethecodecsmoduleandusecodecs.open(path,mode,encoding)whichprovidesthemissingbitinPython. Asforyoureditor,youmustcheckifitofferssomewaytosettheencodingofafile. ThepointofUTF-8istobeabletoencode21-bitcharacters(Unicode)asan8-bitdatastream(becausethat'stheonlythingallcomputersintheworldcanhandle).ButsincemostOSspredatetheUnicodeera,theydon'thavesuitabletoolstoattachtheencodinginformationtofilesontheharddisk. ThenextissueistherepresentationinPython.Thisisexplainedperfectlyinthecommentbyheikogerlach.YoumustunderstandthatyourconsolecanonlydisplayASCII.InordertodisplayUnicodeoranything>=charcode128,itmustusesomemeansofescaping.Inyoureditor,youmustnottypetheescapeddisplaystringbutwhatthestringmeans(inthiscase,youmustentertheumlautandsavethefile). Thatsaid,youcanusethePythonfunctioneval()toturnanescapedstringintoastring: >>>x=eval("'Capit\\xc3\\xa1n\\n'") >>>x 'Capit\xc3\xa1n\n' >>>x[5] '\xc3' >>>len(x[5]) 1 Asyoucansee,thestring"\xc3"hasbeenturnedintoasinglecharacter.Thisisnowan8-bitstring,UTF-8encoded.TogetUnicode: >>>x.decode('utf-8') u'Capit\xe1n\n' GreggLindasked:Ithinktherearesomepiecesmissinghere:thefilef2contains:hex: 0000000:43617069745c7863335c7861316eCapit\xc3\xa1n codecs.open('f2','rb','utf-8'),forexample,readsthemallinaseparatechars(expected)IsthereanywaytowritetoafileinASCIIthatwouldwork? Answer:Thatdependsonwhatyoumean.ASCIIcan'trepresentcharacters>127.Soyouneedsomewaytosay"thenextfewcharactersmeansomethingspecial"whichiswhatthesequence"\x"does.Itsays:Thenexttwocharactersarethecodeofasinglecharacter."\u"doesthesameusingfourcharacterstoencodeUnicodeupto0xFFFF(65535). Soyoucan'tdirectlywriteUnicodetoASCII(becauseASCIIsimplydoesn'tcontainthesamecharacters).Youcanwriteitasstringescapes(asinf2);inthiscase,thefilecanberepresentedasASCII.OryoucanwriteitasUTF-8,inwhichcase,youneedan8-bitsafestream. Yoursolutionusingdecode('string-escape')doeswork,butyoumustbeawarehowmuchmemoryyouuse:Threetimestheamountofusingcodecs.open(). Rememberthatafileisjustasequenceofbyteswith8bits.Neitherthebitsnorthebyteshaveameaning.It'syouwhosays"65means'A'".Since\xc3\xa1shouldbecome"à"butthecomputerhasnomeanstoknow,youmusttellitbyspecifyingtheencodingwhichwasusedwhenwritingthefile. Share Improvethisanswer Follow editedMay23,2017at11:47 CommunityBot 111silverbadge answeredJan29,2009at16:54 AaronDigullaAaronDigulla 314k104104goldbadges583583silverbadges807807bronzebadges 1 Ithinktherearesomepiecesmissinghere:thefilef2contains:hex:0000000:43617069745c7863335c7861316e0aCapit\xc3\xa1n.codecs.open('f2','rb','utf-8'),forexample,readsthemallinaseparatechars(expected)Isthereanywaytowritetoafileinasciithatwouldwork? – GreggLind Jan29,2009at17:21 Addacomment | 4 The\x..sequenceissomethingthat'sspecifictoPython.It'snotauniversalbyteescapesequence. HowyouactuallyenterinUTF-8-encodednon-ASCIIdependsonyourOSand/oryoureditor.Here'showyoudoitinWindows.ForOSXtoenterawithanacuteaccentyoucanjusthitoption+E,thenA,andalmostalltexteditorsinOSXsupportUTF-8. Share Improvethisanswer Follow editedJan4,2017at18:09 PeterMortensen 30.6k2121goldbadges102102silverbadges124124bronzebadges answeredJan29,2009at15:10 ʞɔıuʞɔıu 45.9k3434goldbadges101101silverbadges145145bronzebadges Addacomment | 3 Youcanalsoimprovetheoriginalopen()functiontoworkwithUnicodefilesbyreplacingitinplace,usingthepartialfunction.Thebeautyofthissolutionisyoudon'tneedtochangeanyoldcode.It'stransparent. importcodecs importfunctools open=functools.partial(codecs.open,encoding='utf-8') Share Improvethisanswer Follow editedJan4,2017at18:47 PeterMortensen 30.6k2121goldbadges102102silverbadges124124bronzebadges answeredDec8,2016at3:22 hipertrackerhipertracker 2,3462424silverbadges1414bronzebadges 0 Addacomment | 1 IwastryingtoparseiCalusingPython2.7.9: fromicalendarimportCalendar ButIwasgetting: Traceback(mostrecentcalllast): File"ical.py",line92,inparse print"{}".format(e[attr]) UnicodeEncodeError:'ascii'codeccan'tencodecharacteru'\xe1'inposition7:ordinalnotinrange(128) anditwasfixedwithjust: print"{}".format(e[attr].encode("utf-8")) (Nowitcanprintlikéáböss.) Share Improvethisanswer Follow editedJan4,2017at18:45 PeterMortensen 30.6k2121goldbadges102102silverbadges124124bronzebadges answeredMay10,2016at12:49 AlexxRocheAlexxRoche 2,98711goldbadge3434silverbadges3838bronzebadges Addacomment | -2 Ifoundthemostsimpleapproachbychangingthedefaultencodingofthewholescripttobe'UTF-8': importsys reload(sys) sys.setdefaultencoding('utf8') anyopen,printorotherstatementwilljustuseutf8. WorksatleastforPython2.7.9. Thxgoestohttps://markhneedham.com/blog/2015/05/21/python-unicodeencodeerror-ascii-codec-cant-encode-character-uxfc-in-position-11-ordinal-not-in-range128/(lookattheend). Share Improvethisanswer Follow answeredDec17,2019at14:49 dr0idr0i 2,25011goldbadge1818silverbadges3434bronzebadges 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?Browseotherquestionstaggedpythonunicodeutf-8iooraskyourownquestion. TheOverflowBlog HowtoearnamillionreputationonStackOverflow:beofservicetoothers Therightwaytojobhop(Ep.495) FeaturedonMeta BookmarkshaveevolvedintoSaves Inboximprovements:markingnotificationsasread/unread,andafiltered... Revieweroverboard!Orarequesttoimprovetheonboardingguidancefornew... CollectivesUpdate:RecognizedMembers,Articles,andGitLab Shouldweburninatethe[script]tag? Linked 1 FPDFencodingerrorwhenreadingaUTF8txtfileinPython 0 HowtowritestringsinUnicodetoatextfileinPython? 0 Gettinnon-englishtextfromhtmldoc 116 CharacterreadingfromfileinPython 46 HowtoopenhtmlfilethatcontainsUnicodecharacters? 4 Python2.7UnicodeDecodeError:'ascii'codeccan'tdecodebyte 7 HowtowriteChinesecharacterstofilebypython 10 HandlingfrenchlettersinPython 4 codecs.ascii_decode(input,self.errors)[0]UnicodeDecodeError:'ascii'codeccan'tdecodebyte0xc2inposition318:ordinalnotinrange(128) 2 Howtohandleunknowencoding Seemorelinkedquestions Related 2414 StaticclassvariablesandmethodsinPython 609 UTF-8,UTF-16,andUTF-32 709 Whatisthebestwaytoremoveaccents(normalize)inaPythonunicodestring? 674 WhatisthedifferencebetweenUTF-8andUnicode? 974 What'sthedifferencebetweenUTF-8andUTF-8withBOM? 475 WhatareUnicode,UTF-8,andUTF-16? 1126 HowdoIimportotherPythonfiles? 1262 IfPythonisinterpreted,whatare.pycfiles? 2112 WhyisreadinglinesfromstdinmuchslowerinC++thanPython? HotNetworkQuestions InD&D3.5,whathappenswhenyouplopaheadbandofintellectonananimal? Botchingcrosswindlandings Single-rowSettingstable:prosandconsofJoinsvsscalarsubqueries ConvertanintegertoIEEE754float CanIuseaspritesheetfromanexistingvideogameformypromotionalreel? Whoorwhatis"Nampat"inthechantoftheOrcsintheRingsofPower? Myfavoriteanimalisa-singularandpluralform HowdoIdownloadmacOSMontereyonunsupportedMac? Howtotellifmybikehasanaluminumframe StrangeFruitfromTomatoPlant IsdocumentingabigprojectwithUMLDiagramsneeded,goodtohaveorevennotpossible? Howtoplug2.5mm²strandedwiresintoapushwirewago? Areyougettingtiredofregularcrosswords? 2016PutnamB6difficultsummationproblem Whydostringhashcodeschangeforeachexecutionin.NET? MakeaCourtTranscriber Howtosimplifyapurefunction? Doyoupayforthebreakfastinadvance? Howdocucumbershappen?Whatdoes"verypoorlypollinatedcucumber"meanexactly?Howcanpollinationbe"uneven"? ArethereanyspellsotherthanWishthatcanlocateanobjectthroughleadshielding? PacifistethosblockingmyprogressinStellaris sshhowtoallowaverylimiteduserwithnohometologinwithpubkey Isitcorrecttochangetheverbto"being"in"Despitenoonewashurtinthisincident…"? WhatistheAmericanequivalentof"Icalledmymomtoaskafterher"? morehotquestions Questionfeed SubscribetoRSS Questionfeed TosubscribetothisRSSfeed,copyandpastethisURLintoyourRSSreader. lang-py Yourprivacy Byclicking“Acceptallcookies”,youagreeStackExchangecanstorecookiesonyourdeviceanddiscloseinformationinaccordancewithourCookiePolicy. Acceptallcookies Customizesettings
延伸文章資訊
- 1Python读写utf-8的文本文件 - 杨仕航的博客
Python内置的open方法可以读写文件,但是只是ANSI字符编码或者二进制的形式 ... 1.txt', 'w', encoding='utf-8'); f.write(u'这才是utf-8...
- 2Unicode HOWTO — Python 3.10.7 documentation
- 3open('file.txt', mode='wt', encoding='utf-8') - YouTube
- 4Python 的Big5 與UTF-8 檔案編碼轉換程式教學 - Office 指南
介紹如何使用簡單的Python 程式處理Big5 與UTF-8 檔案的編碼轉換問題。 ... 開啟Big5 輸入檔案 inFile = open("big5_input.txt", "r", e...
- 5Python 3 Tutorial 第二堂(1)Unicode 支援、基本I/O
filename = input('檔名:') file = open(filename, 'r', encoding='UTF-8') content = file.read() file.c...