Python write lines to a UCS-2 LE BOM encoded text file
文章推薦指數: 80 %
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
延伸文章資訊
- 1Why Python 3 doesn't write the Unicode BOM - Peter Bloomfield
This post outlines when and why Python doesn't output the BOM, ... You'll see UTF-16 (or UCS-2) L...
- 2Python 3: reading UCS-2 (BE) file - Stack Overflow
Open it with encoding='utf16' . If there is no BOM (the Byte order mark, 2 bytes at the start, fo...
- 3Python write lines to a UCS-2 LE BOM encoded text file
Try writing an explicit BOM: with open(textpath, "w", encoding='utf-16-le') as f: f.write('\ufeff...
- 4python读取UCS-2 little endian(utf-16-le) 格式的文件- 仙羡1118
今天开发帮忙写了一个读取windows文件签名的小程序,并把签名信息写到txt文件中,用UE打开发现居然是UCS-2 LE编码,几经查找,如下是读取的文本。
- 5谈谈Unicode编码,简要解释UCS、UTF、BMP、BOM等名词
谈谈Unicode编码,简要解释UCS、UTF、BMP、BOM等名词 · 问题一 · 问题二 · 0、big endian和little endian · 1、字符编码、内码,顺带介绍汉字编码 ...