utf-16le[BOM] to utf-8 file solution - GitHub
文章推薦指數: 80 %
http://stackoverflow.com/questions/22459020/python-decode-utf-16-file-with-bom. import codecs. encoded_text = open('utf16lebom_file', 'rb').read() #you ... Skiptocontent Allgists BacktoGitHub Signin Signup Sign in Sign up {{message}} Instantlysharecode,notes,andsnippets. ctjoy/utf16leBOM_to_utf8.py LastactiveMar25,2018 Star 0 Fork 0 Star Code Revisions 2 Embed Whatwouldyouliketodo? Embed Embedthisgistinyourwebsite. Share Copysharablelinkforthisgist. Clonevia HTTPS ClonewithGitorcheckoutwithSVNusingtherepository’swebaddress. LearnmoreaboutcloneURLs DownloadZIP utf-16le[BOM]toutf-8filesolution Raw utf16leBOM_to_utf8.py ThisfilecontainsbidirectionalUnicodetextthatmaybeinterpretedorcompileddifferentlythanwhatappearsbelow.Toreview,openthefileinaneditorthatrevealshiddenUnicodecharacters. LearnmoreaboutbidirectionalUnicodecharacters Showhiddencharacters #http://stackoverflow.com/questions/22459020/python-decode-utf-16-file-with-bom importcodecs encoded_text=open('utf16lebom_file','rb').read()#youshouldreadinbinarymodetogettheBOMcorrectly bom=codecs.BOM_UTF16_LE#printdir(codecs)forotherencodings assertencoded_text.startswith(bom)#makesuretheencodingiswhatyouexpect,otherwiseyou'llgetwrongdata encoded_text=encoded_text[len(bom):]#stripawaytheBOM decoded_text=encoded_text.decode('utf-16le') f=open('utf8_file','wb') f.write(decoded_text.encode('utf8')) f.close() Signupforfree tojointhisconversationonGitHub. Alreadyhaveanaccount? Signintocomment Youcan’tperformthatactionatthistime. Yousignedinwithanothertaborwindow.Reloadtorefreshyoursession. Yousignedoutinanothertaborwindow.Reloadtorefreshyoursession.
延伸文章資訊
- 1Python: 關於Unicode 的BOM - 傑克! 真是太神奇了! - 痞客邦
至於UTF-8 編碼: 是將Unicode 編碼的字串資料轉成8 位元序列(轉換規則如下表: UTF-8 ... 寫檔時, 要依據需求自己先寫入一個BOM ( write('\ufeff') ).
- 2Why Python 3 doesn't write the Unicode BOM - Peter Bloomfield
According to the Python documentation on reading and writing Unicode data: Some encodings, such a...
- 3Python Utf8 Bom? Best 5 Answer - Barkmanoil.com
Why Python 3 doesn't write the Unicode BOM – Peter Bloomfield. What is UTF-16 Le BOM? What is BOM...
- 4Python:str file,open,read,write,close,and utf8 BOM resloving
Python:str file,open,read,write,close,and utf8 BOM resloving - utf8_decode.py.
- 5[python] 解決生成csv file編碼問題(with BOM) - JysBlog
當我們使用UTF-8生成csv時,並未在header生成BOM訊息,所以Excel會依照Unicode編碼讀取,就會有亂碼產生。 實作. 下面是簡單的生成csv的python程式:.