2017-09-13 9 views
1

未知のエンコーディングの異なるテキストファイルがあります。今では、最初にエンコーディングを検出するためにバイナリとしてファイルをオープンし、エンコーディングで再びオープンする必要があります。Pythonでファイルエンコーディングを処理するより良い方法はありますか?

bf = open(f, 'rb') 
    code = chardet.detect(bf.read())['encoding'] 
    print(f + ' : ' + code) 
    bf.close() 
    with open(f, 'r', encoding=code) as source: 
    texts = extractText(source.readlines()) 
    source.close() 
    with open(splitext(f)[0] + '_texts.txt', 'w', encoding='utf-8') as dist: 
    dist.write('\n\n'.join('\n'.join(x) for x in texts)) 
    dist.close() 

この問題を解決する方法はありますか?

+0

? –

+0

このリンクを見てください。 あなたが探しているものに役立つかもしれません。 https://stackoverflow.com/questions/18263136/how-to-deal-with-unknown-encoding-when-scraping-webpages –

+0

@EricDuminilさまざまなソフトウェア用のファイルです。エンコーディングを推測する方法はありません。 – Jacob

答えて

2

代わりに、ファイルを再度開くと再読の、あなたはちょうどあなたがすでに読み込まれたテキストをデコードできます。それらのファイルから来るのか

with open(filename, 'rb') as fileobj: 
    binary = fileobj.read() 
probable_encoding = chardet.detect(binary)['encoding'] 
text = binary.decode(probable_encoding) 
関連する問題