A
A
alexkomp2019-11-03 22:51:25
Python
alexkomp, 2019-11-03 22:51:25

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

1 answer(s)
R
Ruslan, 2019-11-04
@alexkomp

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')))

UPDATE
jointly concluded that I misunderstood the question initially.
The error is not in python itself, but in opening the csv file in excel.
To open csv, you need to import it into excel and specify utf-8 encoding in the wizard.
But this needs to be done with every csv import.
Therefore, to obtain data for excel with a normal encoding, it is better to write data not in csv, but in a full-fledged xlsx file using the openxl module. It allows you to create a full-fledged excel book and write data directly there. After saving the file with this module, the encoding in excel should open immediately normally.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question