Python: gewusst wie: konvertieren von Windows-1251 auf Unicode?
Ich versuche, die Datei konvertieren, Inhalte von Windows-1251 (Kyrillisch) auf Unicode mit Python. Ich fand diese Funktion, aber es funktioniert nicht.
#!/usr/bin/env python
import os
import sys
import shutil
def convert_to_utf8(filename):
# gather the encodings you think that the file may be
# encoded inside a tuple
encodings = ('windows-1253', 'iso-8859-7', 'macgreek')
# try to open the file and exit if some IOError occurs
try:
f = open(filename, 'r').read()
except Exception:
sys.exit(1)
# now start iterating in our encodings tuple and try to
# decode the file
for enc in encodings:
try:
# try to decode the file with the first encoding
# from the tuple.
# if it succeeds then it will reach break, so we
# will be out of the loop (something we want on
# success).
# the data variable will hold our decoded text
data = f.decode(enc)
break
except Exception:
# if the first encoding fail, then with the continue
# keyword will start again with the second encoding
# from the tuple an so on.... until it succeeds.
# if for some reason it reaches the last encoding of
# our tuple without success, then exit the program.
if enc == encodings[-1]:
sys.exit(1)
continue
# now get the absolute path of our filename and append .bak
# to the end of it (for our backup file)
fpath = os.path.abspath(filename)
newfilename = fpath + '.bak'
# and make our backup file with shutil
shutil.copy(filename, newfilename)
# and at last convert it to utf-8
f = open(filename, 'w')
try:
f.write(data.encode('utf-8'))
except Exception, e:
print e
finally:
f.close()
Wie kann ich das tun?
Danke
- Welche Codierung meinst du mit Unicode?
- die Beurteilung durch den code der ausgegeben werden soll UTF-8.
Du musst angemeldet sein, um einen Kommentar abzugeben.
Ist das, was Sie beabsichtigen zu tun?
Dies ist nur eine Vermutung, da Sie nicht angegeben haben, was meinen Sie mit "funktioniert nicht".
Wenn die Datei erzeugt wird, die richtig scheint aber sinnlose Zeichen enthalten, wahrscheinlich die Anwendung, die Sie gerade anzeigen, es mit der nicht erkennt, dass es enthält UTF-8. Man muss hinzufügen, eine BOM an den Anfang der Datei - das 3 bytes
0xEF,0xBB,0xBF
(unencoded).Wenn Sie die
codecs
Modul, die Datei zu öffnen, wird es nicht die Umstellung auf Unicode für Sie, wenn Sie aus der Datei gelesen. E. g.:Macht dies nur Sinn, wenn Sie arbeiten mit Daten der Datei in Python. Wenn Sie versuchen, eine Datei konvertieren, die von einer Codierung zur anderen auf das Dateisystem (das ist an dem script was du gepostet hast versucht zu tun), müssen Sie angeben, dass eine tatsächliche Codierung, da Sie nicht schreiben können eine Datei in "Unicode".
'rb'
als der Modus. Überschreiben Sie es'r'
ist fast nie das, was Sie tun möchten.encodings.cp1251.StreamReader
zum Lesen der Eingabe.'cp1251'
argument, dann wird es sich öffnen und im text-Modus. Nur aus Neugier, warum nicht'rb'
ausdrücklich oder verwenden Siecodecs.open('input.txt', encoding='cp1251')
und lassen Sie den Standardwert erledigen?codecs.open('input.txt', encoding='cp1251')
ist der Richtige Weg, es zu tun... die Aktualisierung meiner Antwort für die Nachwelt.