f | import sys | f | import sys |
| if len(sys.argv) > 1: | | if len(sys.argv) > 1: |
n | with open(sys.argv[1], 'rb') as f: | n | with open(sys.argv[1], 'rb') as file: |
| data = f.read() | | data = file.read() |
| else: | | else: |
| data = sys.stdin.buffer.read() | | data = sys.stdin.buffer.read() |
n | available_encodings = ['KOI8-R', 'CP1251', 'MACCYRILLIC', 'CP866', 'ISO- | n | encodings = ['KOI8-R', 'CP1251', 'MACCYRILLIC', 'CP866', 'ISO-8859-5', ' |
| 8859-5', 'CP855'] | | CP855'] |
| fl = False | | found = False |
| for src_encoding in available_encodings: | | for decode_enc in encodings: |
| for temp_encoding in available_encodings: | | for intermediate_enc in encodings: |
| for dest_encoding in available_encodings: | | for final_enc in encodings: |
| try: | | try: |
n | intermediate_text = data.decode(src_encoding) | n | step1 = data.decode(decode_enc) |
| encoded_temp = intermediate_text.encode(temp_encoding) | | step2 = step1.encode(intermediate_enc) |
| final_text = encoded_temp.decode(dest_encoding) | | result = step2.decode(final_enc) |
| if 'Зимбабве' in final_text: | | if 'Зимбабве' in result: |
| print(final_text) | | print(result) |
| fl = True | | found = True |
| break | | break |
| except (UnicodeDecodeError, UnicodeEncodeError): | | except (UnicodeDecodeError, UnicodeEncodeError): |
| continue | | continue |
n | if fl: | n | if found: |
| break | | break |
n | if fl: | n | if found: |
| break | | break |
t | if not fl: | t | if not found: |
| print('Не удалось найти подходящий текст.') | | print('Подходящий текст не найден.') |