You want to save a specific set of data in a CSV file. The file must be in cp1251 encoding.
Pseudocode (extra is removed):
#!/usr/bin/python3 # -*- Coding: utf-8 -*- import csv numbers = %Π·Π°ΠΏΡΠ°ΡΠΈΠ²Π°Π΅ΠΌ_ΠΈΠ·_ΠΌΠΎΠ½Π³ΠΈ_Π½Π°Π±ΠΎΡ_Π΄Π°Π½Π½ΡΡ
% report_filename = '/tmp/report.csv' with open(report_filename, 'w', encoding='cp1251', newline='') as csvfile: csv_file = csv.writer(csvfile, delimiter=';') for number in numbers: try: csv_file.writerow([number, %ΡΡΡΠΎΠΊΠ°_Ρ_ΠΊΠΈΡΠΈΠ»Π»ΠΈΡΠ΅ΠΉ%, %Π΅ΡΡ_ΠΎΠ΄Π½Π°_ΡΡΡΠΎΠΊΠ°_Ρ_ΠΊΠΈΡΠΈΠ»Π»ΠΈΡΠ΅ΠΉ%]) except Exception as msg: print(number, ': ', msg) continue The code works, lines where there are non-convertible characters are skipped, and you would not need to skip them. I get the encoding is set only when opening a file for writing. I tried various options with encode () and decode (), str.decode () in the third Python, in general, no, there was no success in general. How to make it so that it ignores invalid characters and correctly transcode utf-8 lines to cp1251?
Python 3.5.3
errors='ignore'orerrors='replace'docs.python.org/3/library/functions.html#open - andreymal