2017-12-05 1 views
0

Dies ist mein Code:Antiword mit UTF-8 in Python

from subprocess import Popen, PIPE 
cmd = ['antiword', 'tbhocbong151.doc'] 
p = Popen(cmd, stdout=PIPE) 
stdout, stderr = p.communicate() 
print(stdout.decode('utf-8', 'ignore')) 

Ich habe Inhalt in der Datei Wort wie folgt aus: "Chào bạn"

aber wenn ich generierte Ausgabe ist: "Ch?o b?n"

Wie kann Ich repariere es, um wie Input auszugeben? Danke für Ihre Hilfe

Antwort

1

Ich glaube, dass das Problem ist, dass das Gebietsschema nicht ordnungsgemäß festgelegt ist, wenn antiword ausgeführt wird. Versuchen Sie folgendes:

import os 
from subprocess import Popen, PIPE 
myenv = dict(os.environ) 
if 'LC_ALL' in myenv: 
    del myenv['LC_ALL'] 
myenv['LANG'] = 'en_US.UTF-8' 
cmd = ['antiword', 'tbhocbong151.doc'] 
p = Popen(cmd, stdout=PIPE, env=myenv) 
stdout, stderr = p.communicate() 
print(stdout.decode('utf-8', 'ignore')) 

Wenn das nicht funktioniert, versuchen Sie die LANG env Variable in Ihrer Shell Einstellung vor Ihrem Python-Programm ausgeführt wird; z.B. dabei:

export LANG=en_US.UTF-8