Python write lines to a UCS-2 LE BOM encoded text file

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

Try writing an explicit BOM: with open(textpath, "w", encoding='utf-16-le') as f: f.write('\ufeff') for line in newlines: f.write(line) ... ANYCODINGS.COM ☰ Home Categories HTML Javascript CSS ReactJs AboutUs ContactUs PrivacyPolicy Terms&Conditions ☼ ☾ PythonwritelinestoaUCS-2LEBOMencodedtextfileQuestions:PythonwritelinestoaUCS-2LEBOMencodedtextfile2022-10-08T20:33:57+00:002022-10-08T20:33:57+00:00903 IhaveanarrayoftextstringsinPythonanycodings_encoding3.7. NowIwanttowritethemalltoatextfile.anycodings_encodingTheproblemis,thattextfilehastobeinanycodings_encodingencodingUCS-2LEBOM(thatswhatitsaysanycodings_encodingaboutitsencodinginNotepad++),otherwiseanycodings_encodingthefilewon'tworkinfurtherprocessing. HowdoIwritethetextstringstothefileanycodings_encodinginthatencodingwhilethestringsstayinganycodings_encodingreadable? withopen(textpath,"w",encoding='utf-16-le')asf: forlineinnewlines: f.write(line) Thisdoesnotworkbecauseitgeneratesanycodings_encodinggibberishtext... Admins PYTHONENCODINGUCS2TotalAnswers1 24 Answers1:ofPythonwritelinestoaUCS-2LEBOMencodedtextfile TrywritinganexplicitBOM: withopen(textpath,"w",encoding='utf-16-le')asf: f.write('\ufeff') forlineinnewlines: f.write(line) #Perhapsyoualsoneedtoaddanewlineaftereachline? f.write('\n') Obviouslyrevertthelastadditionifanycodings_ucs2yourlinesalreadyhavenewlines. 0 2022-10-08T20:33:57+00:002022-10-08T20:33:57+00:00AnswerLink mRahman Topratedtopics IssueswithdifferentliststyletypeforParentandchildusingCSSListCounterWhatisthepurposeofeslint-plugin-prettier? Throttlewithlodashisn'tthrottlingusingReactJSExtractEntirearrayobjectfromjsonbody ConnectingtoMySQLserverinascriptreturnserror,butitworksfineinaterminalQueryfailedafterupgradePostgresfrom11to12/13/14Next.jshowtoworkwithsheduledfunctions?Rscript:identifythemostrecentfileinadirectorythatsatisfyconditionHowtofixpositionwhilerefreshingthewebpageinsideascrollingtable?Npm/gitfailswith:refusingtoworkwithcredentialmissinghostfieldLoadrunnerweb_convert_paramfunctionnotworkingproperlyEffectivetypeinpackedrawdataHowtogroupbywithtwofieldinJavascriptDon'tdisplaynext/imagecomponentwhentheimagedoesn'texistImageisnotshowinginthewebpageeventhoughitisopenedseparately"ViewParameterInfo"featurenotworkinginInteliJIDEAPact:MinimalExampletotestAPI(JVM)HowcanInotifytheclientwhenasyncrequestsbetweenmicroservicesaredone?Howaboutselectingchildreninchildreninchildren...morecompromisedIsthereanywaybywhichwecanchangethevaluesofenvironmentvariablesdynamicallyinanAngular10application?Enabling/DisablingIntelliJ'sfancy≠(notequalto)operatorJestnotexitingproperlywhenusingRTK-QueryuseLazyQuerywithReactNativeSqlQuerytogettheoutsideofoverlappingdateintervalsChangingtheexecuting(R)scriptsenttosbatchinslurmduringrunLegendModificationinGnuplot4.6AzurefunctionConsumemessagesfromanEventhubandPOSTthesametoaRESTAPIBeginnerquestionaboutdictionariesandfunctioninpythonHowdoIgetstringfromtheaddressofchar*?HowtointegrateanSRT(securereliabletransport)streamplayerplaybacktomyprojectiOS?Ejs-lineargaugemajorticksandminorticksmiddleCanwerestrictoperationofGitrepolink/unlinkinAzureDataFactory(ADF)?HowtobreakupStreamReaderresponseStreamintosegmentstobuildarray/objectsWherewithmultiplelikenotworkingincodignitorFilterrowsthatmeettwodifferentialconditionsShowandhideselecteddivelementvia.map()HowcanIgetalltheroadstravelletfromagooglemapsroutelink?HowtochangethemonthnamecolumntomonthnumbercolumnindataframeinpandasBindingaDataTriggervaluetothisinstanceofDataTemplateHowtoisolatecauseofEntityFrameworkerror2062:NomappingspecifiedforinstancesoftheEntitySetandAssociationSetDockerpullingunauthorized-DockerandGithubactionsCannotconnecttopgAdmin4whenrunincontainerNext.jsfunctionnotworkingoniPhonesafariDisplaymultiplerandomimagesratherthanjusttheoneXamppquestion.-isinstalledasawindowsservice?SettingfactfromjsonwithhyphensDeletePictureFromCellImportingLeankitDataIntoAzureDevOpsHowdoIturnalistofwordsintoaflattenedlistofcharactersinC#?RethrowexceptioninforloopAdviceonhandlingroutingwhenusingsubdomains? Top ©2022 ANYCODINGS.COM-AllRightsReserved. AboutUs ContactUs DMCA PrivacyPolicy Disclaimer Terms&Conditions



請為這篇文章評分?