-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_encode.py
32 lines (27 loc) · 1008 Bytes
/
process_encode.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
#!/usr/bin/env python3
"""Модуль, осуществляющий работу с различными кодировками"""
from chardet.universaldetector import UniversalDetector
def get_decode_text(file):
"""Метод, осуществляющий преобразование текстового файла"""
detector = UniversalDetector()
with open(file, 'rb') as fh:
for line in fh:
detector.feed(line)
if detector.done:
break
detector.close()
encode = detector.result['encoding']
encode_file = file.encode(encode)
decode = 'utf-8'
decode_file = encode_file.decode(decode)
with open(decode_file, 'rb') as f:
for line in f:
detector.feed(line)
if detector.done:
break
detector.close()
with open(decode_file, 'r') as new_f:
lines = new_f.readlines()
text = lines[0]
red_type = lines[1]
return text[:-1], red_type