Answer the question
In order to leave comments, you need to log in
How can I make the CSV file output Cyrillic instead of hieroglyphs when executing the code?
Wrote parsing for RIA Novosti in Python.
The parser works, but when I open the file, all Cyrillic is displayed as hieroglyphs.
Here is the code for opening CSV for writing.
How to make Cyrillic, not hieroglyphs, displayed after writing to CSV?
def file_w(news):
with open('parser_news.csv', 'w', encoding='utf-8') as file:
a_pen = csv.writer(file)
a_pen.writerow(('Название', 'Ссылка на статью', 'Дата', 'Ссылка на картинку', 'Контент'))
for new in news:
a_pen.writerow((new['title'], new['href'], new['date'], new['img'], new['content']))
news = RIA_NEWS(base_url, headers)
file_w(news)
Answer the question
In order to leave comments, you need to log in
It is necessary to convert to utf-8 inside python.
As I understand it, this applies to new['title'] and new['content'].
You need to add decode to them in your loop.
I.e:
for new in news:
a_pen.writerow((new['title'].decode("utf-8"), new['href'], new['date'], new['img'], new['content'].decode('utf-8')))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question