A
A
AleDv2018-04-18 14:44:43
Python
AleDv, 2018-04-18 14:44:43

How to write Cyrillic to CSV in Python?

Hello everyone, I've encountered writing Cyrillic in a CSV file. there is this code:

# -*- coding: utf-8 -*-
....
    with open(filename, 'w+', encoding='utf8', newline='') as file:
        writer = csv.writer(file, delimiter=';')
        count_elements = len(data['urls'])
        for i in range(1, count_elements):
            writer.writerow([
                data['titles'][i],
                data['titles'][i],
                int(data['costs'][i]),
                data['address'][i],
                data['urls'][i],
            ])

As a result, I get a krakozyabra in the file:
5ad72f911334c025099332.png
How to write the Cyrillic alphabet correctly?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
B
BlOr, 2018-04-18
@BlOr

data['urls'][i].encode("cp1251")

V
Vladimir Kuts, 2018-04-18
@fox_12

Use unicodecsv

N
NaName, 2018-04-18
@NaName

with open(filename, 'w+', encoding='utf8', newline='')
replace
with with open(filename, 'w+', newline='')
or
with open(filename, 'w+', encoding='cp1251 ',newline='')

S
Simon Osipov, 2018-04-20
@SimonOsipov

Take note of the encoding you are using:
encoding='utf8'
Take note of this question . There is an answer there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question