-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdecoder.py
43 lines (39 loc) · 1.26 KB
/
decoder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def decoder(data):
westeurcnt = 0
cyrcnt = 0
othercnt = 0
#print [data]
b = 0
while b < len(data):
if data[b] == "\xc2" or data[b] == "\xc3":
westeurcnt += 1
b += 2
elif data[b] == "\xd0" or data[b] == "\xd1":
cyrcnt += 1
b += 2
else:
#print b, [data[b]]
b += 1
othercnt += 1
#print westeurcnt, cyrcnt, othercnt, '/', len(data)
data = data.decode("utf-8")
#print len(data)
if westeurcnt > cyrcnt and (othercnt / westeurcnt) < 1:
try:
data1252 = data.encode("cp1252")
except UnicodeEncodeError:
try:
datalatin = data.encode("latin-1")
except UnicodeEncodeError:
return data
return datalatin.decode("utf-8")
data = data1252.decode("cp1251")
return data
else:
return data
if __name__ == "__main__":
print [decoder(u"Ìóìèé Òðîëëü — Äåëàé ìåíÿ òî÷íî".encode('utf-8'))]
print [decoder(u"Ололошеньки-лоло".encode('utf-8'))]
print [decoder(u"Ðино - ÐÑÑппа кÑови".encode('utf-8'))]