[討論] file BOM
之前有點事需要 parse file,結果被 file encoding 搞了兩個小時
後來我是這樣做
import os, sys, codecs
def test_file_encoding(file_path):
file_encoding = sys.getfilesystemencoding()
bom_len = 0
with open(file_path, 'r') as f:
head = f.read(5)
if head[:len(codecs.BOM_UTF16_LE)] == codecs.BOM_UTF16_LE:
file_encoding = 'utf-16-le'
bom_len = 1
elif head[:len(codecs.BOM_UTF16_BE)] == codecs.BOM_UTF16_BE:
file_encoding = 'utf-16-be'
bom_len = 1
elif head[:len(codecs.BOM_UTF8)] == codecs.BOM_UTF8:
file_encoding = 'utf-8'
bom_len = 1
return (file_encoding, bom_len)
def parse_file(file_path):
(file_encoding, bom_len) = test_file_encoding(file_path)
with codecs.open(file_path, mode='r', encoding=file_encoding) as f:
f.read(bom_len)
for line in f:
# do my job
雖然這樣是 ok 啦
可是總覺得這問題應該有現成解才對....
請問有沒有不必自己這麼辛苦的方法 XD
--
天地のはざまに迷えし古来より生まれし邪悪な精霊よ
聖なる処女の柔肌に纏いし衣の雷で
汚れも濁りも淀みも凝りも
微塵に砕いて天地に返す!
悔い改めよ!
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.32.118.49
→
01/21 17:20, , 1F
01/21 17:20, 1F
推
01/21 22:35, , 2F
01/21 22:35, 2F
→
01/21 22:36, , 3F
01/21 22:36, 3F
討論串 (同標題文章)
完整討論串 (本文為第 1 之 2 篇):
1
3
Python 近期熱門文章
PTT數位生活區 即時熱門文章