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
延伸文章資訊
- 1將UCS-2 Little Endian(即utf-16)編碼的txt檔案批量轉化為utf-8 ...
折騰了好久,終於搞定了。 參考連結:python使用codecs模組進行檔案操作-讀寫中英文字元- CSDN部落格http://blog.csdn.net ...
- 2python读取UCS-2 little endian(utf-16-le) 格式的文件- 仙羡1118
今天开发帮忙写了一个读取windows文件签名的小程序,并把签名信息写到txt文件中,用UE打开发现居然是UCS-2 LE编码,几经查找,如下是读取的文本。
- 3谈谈Unicode编码,简要解释UCS、UTF、BMP、BOM等名词
谈谈Unicode编码,简要解释UCS、UTF、BMP、BOM等名词 · 问题一 · 问题二 · 0、big endian和little endian · 1、字符编码、内码,顺带介绍汉字编码 ...
- 4Python 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...
- 5UCS-2 LE BOM python pandas - 台部落
UCS-2 LE BOM python pandas. 原創 Eddiewzw 2018-10-20 09:39. encoding='utf-16'. UnicodeDecodeError: ...