Check whether a file contains valid UTF-8. Returns 0 for valid ...

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

Check whether a file contains valid UTF-8. Returns 0 for valid UTF-8, prints an error message to STDOUT and returns 1 for invalid. - check_utf8.py. Skiptocontent Allgists BacktoGitHub Signin Signup Sign in Sign up {{message}} Instantlysharecode,notes,andsnippets. manics/check_utf8.py CreatedFeb4,2013 Star 0 Fork 0 Star Code Revisions 1 Embed Whatwouldyouliketodo? Embed Embedthisgistinyourwebsite. Share Copysharablelinkforthisgist. Clonevia HTTPS ClonewithGitorcheckoutwithSVNusingtherepository’swebaddress. LearnmoreaboutcloneURLs DownloadZIP CheckwhetherafilecontainsvalidUTF-8.Returns0forvalidUTF-8,printsanerrormessagetoSTDOUTandreturns1forinvalid. Raw check_utf8.py ThisfilecontainsbidirectionalUnicodetextthatmaybeinterpretedorcompileddifferentlythanwhatappearsbelow.Toreview,openthefileinaneditorthatrevealshiddenUnicodecharacters. LearnmoreaboutbidirectionalUnicodecharacters Showhiddencharacters #!/usr/bin/envpython #CheckwhetherafilecontainsvalidUTF-8 #Fromhttp://stackoverflow.com/a/3269323 importcodecs importsys importos defcheckFile(filename): try: withcodecs.open(filename,encoding='utf-8',errors='strict')asf: forlineinf: pass return0 exceptIOErrorase: sys.stderr.write('IOerror:%s\n'%e) return2 exceptUnicodeDecodeError: sys.stdout.write('%scontainsinvalidUTF-8\n'%filename) return1 if__name__=='__main__': iflen(sys.argv)!=2: p=sys.argv[0] sys.stderr.write('Usage:'+p[p.rfind('/')+1:]+'\n') sys.exit(2) r=checkFile(sys.argv[1]) sys.exit(r) Signupforfree tojointhisconversationonGitHub. Alreadyhaveanaccount? Signintocomment Youcan’tperformthatactionatthistime. Yousignedinwithanothertaborwindow.Reloadtorefreshyoursession. Yousignedoutinanothertaborwindow.Reloadtorefreshyoursession.



請為這篇文章評分?