Convert UTF-8 with BOM to UTF-8 with no BOM in Python
文章推薦指數: 80 %
Is there a solution that can take any known Python encoding and output as UTF-8 without BOM? edit 1 proposed sol'n from below (thanks!) fp = open('brh-m-157.
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
ConvertUTF-8withBOMtoUTF-8withnoBOMinPython
AskQuestion
Asked
10years,9monthsago
Modified
1monthago
Viewed
154ktimes
101
Twoquestionshere.IhaveasetoffileswhichareusuallyUTF-8withBOM.I'dliketoconvertthem(ideallyinplace)toUTF-8withnoBOM.Itseemslikecodecs.StreamRecoder(stream,encode,decode,Reader,Writer,errors)wouldhandlethis.ButIdon'treallyseeanygoodexamplesonusage.Wouldthisbethebestwaytohandlethis?
sourcefiles:
TueJan17$filebrh-m-157.json
brh-m-157.json:UTF-8Unicode(withBOM)text
Also,itwouldbeidealifwecouldhandledifferentinputencodingwihtoutexplicitlyknowing(seenASCIIandUTF-16).Itseemslikethisshouldallbefeasible.IsthereasolutionthatcantakeanyknownPythonencodingandoutputasUTF-8withoutBOM?
edit1proposedsol'nfrombelow(thanks!)
fp=open('brh-m-157.json','rw')
s=fp.read()
u=s.decode('utf-8-sig')
s=u.encode('utf-8')
printfp.encoding
fp.write(s)
Thisgivesmethefollowingerror:
IOError:[Errno9]Badfiledescriptor
Newsflash
I'mbeingtoldincommentsthatthemistakeisIopenthefilewithmode'rw'insteadof'r+'/'r+b',soIshouldeventuallyre-editmyquestionandremovethesolvedpart.
pythonutf-8utf-16byte-order-mark
Share
Improvethisquestion
Follow
editedJan30,2012at21:15
tzot
89.3k2929goldbadges137137silverbadges201201bronzebadges
askedJan17,2012at16:37
timponetimpone
18.6k3434goldbadges112112silverbadges205205bronzebadges
1
2
Youneedtoopenyourfileforreadingplusupdate,i.e.,withar+mode.AddbtoosothatitwillworkonWindowsaswellwithoutanyfunnylineendingbusiness.Finally,you'llwanttoseekbacktothebeginningofthefileandtruncateitattheend—pleaseseemyupdatedanswer.
– MartinGeisler
Jan17,2012at21:58
Addacomment
|
7Answers
7
Sortedby:
Resettodefault
Highestscore(default)
Trending(recentvotescountmore)
Datemodified(newestfirst)
Datecreated(oldestfirst)
150
Simplyusethe"utf-8-sig"codec:
fp=open("file.txt")
s=fp.read()
u=s.decode("utf-8-sig")
ThatgivesyouaunicodestringwithouttheBOM.Youcanthenuse
s=u.encode("utf-8")
togetanormalUTF-8encodedstringbackins.Ifyourfilesarebig,thenyoushouldavoidreadingthemallintomemory.TheBOMissimplythreebytesatthebeginningofthefile,soyoucanusethiscodetostripthemoutofthefile:
importos,sys,codecs
BUFSIZE=4096
BOMLEN=len(codecs.BOM_UTF8)
path=sys.argv[1]
withopen(path,"r+b")asfp:
chunk=fp.read(BUFSIZE)
ifchunk.startswith(codecs.BOM_UTF8):
i=0
chunk=chunk[BOMLEN:]
whilechunk:
fp.seek(i)
fp.write(chunk)
i+=len(chunk)
fp.seek(BOMLEN,os.SEEK_CUR)
chunk=fp.read(BUFSIZE)
fp.seek(-BOMLEN,os.SEEK_CUR)
fp.truncate()
Itopensthefile,readsachunk,andwritesitouttothefile3bytesearlierthanwhereitreadit.Thefileisrewrittenin-place.Aseasiersolutionistowritetheshorterfiletoanewfilelikenewtover'sanswer.Thatwouldbesimpler,butusetwicethediskspaceforashortperiod.
Asforguessingtheencoding,thenyoucanjustloopthroughtheencodingfrommosttoleastspecific:
defdecode(s):
forencodingin"utf-8-sig","utf-16":
try:
returns.decode(encoding)
exceptUnicodeDecodeError:
continue
returns.decode("latin-1")#willalwayswork
AnUTF-16encodedfilewontdecodeasUTF-8,sowetrywithUTF-8first.Ifthatfails,thenwetrywithUTF-16.Finally,weuseLatin-1—thiswillalwaysworksinceall256bytesarelegalvaluesinLatin-1.YoumaywanttoreturnNoneinsteadinthiscasesinceit'sreallyafallbackandyourcodemightwanttohandlethismorecarefully(ifitcan).
Share
Improvethisanswer
Follow
editedJul18,2018at20:33
200_success
7,10411goldbadge4242silverbadges7171bronzebadges
answeredJan17,2012at16:47
MartinGeislerMartinGeisler
72k2525goldbadges168168silverbadges226226bronzebadges
2
1
hmm,iupdatedthequestioninedit#1withsamplecodebutgettingabadfiledescriptor.thxforanyhelp.Tryingtofigurethisout.
– timpone
Jan17,2012at17:29
2
seemsgotAttributeError:'str'objecthasnoattribute'decode'.SoIfinallyusedthecodeaswithopen(filename,encoding='utf-8-sig')asf_content:,thendoc=f_content.read()anditworkedforme.
– clement116
Apr20,2021at19:21
Addacomment
|
78
InPython3it'squiteeasy:readthefileandrewriteitwithutf-8encoding:
s=open(bom_file,mode='r',encoding='utf-8-sig').read()
open(bom_file,mode='w',encoding='utf-8').write(s)
Share
Improvethisanswer
Follow
editedOct29,2015at19:30
the
20k1111goldbadges6565silverbadges9999bronzebadges
answeredOct23,2015at2:57
GengJiawenGengJiawen
8,39422goldbadges4545silverbadges3737bronzebadges
0
Addacomment
|
7
importcodecs
importshutil
importsys
s=sys.stdin.read(3)
ifs!=codecs.BOM_UTF8:
sys.stdout.write(s)
shutil.copyfileobj(sys.stdin,sys.stdout)
Share
Improvethisanswer
Follow
answeredJan17,2012at17:03
newtovernewtover
30.3k1111goldbadges8080silverbadges8888bronzebadges
2
1
canyouexplainhowthiscodeiswork?$remove_bom.py
延伸文章資訊
- 1BOM 的去除方式:分別使用vim, Python, 及bash - Kirin
BOM 的去除方式:分別使用vim, Python, 及bash. 0. Kirin written 10 個月ago. 最後更新日期:2022 年01 月3 日. BOM 是Byte Orde...
- 2Python flat bill-of-material program based on Excel files - GitHub
A Python program for flattening a layered bill-of-material (BOM) based on Excel files. Part quant...
- 3在Python中將帶BOM的UTF - 程式人生
我想將它們(理想情況下)轉換為沒有BOM的UTF-8。似乎 codecs.StreamRecoder(stream, encode, decode, Reader, Writer, errors...
- 4[python] 解決生成csv file編碼問題(with BOM) - JysBlog
當我們使用UTF-8生成csv時,並未在header生成BOM訊息,所以Excel會依照Unicode編碼讀取,就會有亂碼產生。 實作. 下面是簡單的生成csv的python程式:.
- 5python 讀取帶BOM的utf-8格式檔案 - 程式人生
微軟在UTF-8中使用BOM(Byte order mark)是因為這樣可以將UTF-8和ASCII等 ... 比如很多現代指令碼語言,例如python,其直譯器本身是能處理BOM的, ...