O
O
olafars2018-08-01 13:44:00
Python
olafars, 2018-08-01 13:44:00

How to solve the problem of Cyrillic characters in csv?

There is data in mongodb from which I collect csv file.
CSV is collected, but Russian characters are there in the form of krakozyabr.
The csv creation code is the following:

from pymongo import MongoClient
import csv

filename = 'products.csv'
with open(filename, "w", encoding="utf-8", newline="") as file:
    columns = ["url", "name", "price", "manufacturer", "country"]
    writer = csv.DictWriter(file, fieldnames=columns)
    client = MongoClient('localhost', 27017)
    db = client.site
    collection = db.products
    rows = collection.find()
    for row in rows:
        del row['_id']
        if row['manufacturer'] == "":
            row['manufacturer'] = "-"
        if row['country'] == "":
            row['country'] = "-"
        writer.writerow(row)
        print(row)

Ubuntu Linux 18.04 machine, Python 3.6.5, PyCharm, Mongodb v4.0.0
The output characters are as follows:
РњРђРљРР—-ФАРМА,РћРћРћ on our - MAKIZ-PHARMA, LLC
In Ubuntu 18.04 everything is fine, I open it on other machines ( Windows/OS X) in MS Office Excel - no, what's wrong with csv? Thus in sublime text opens normally.
Help who than can?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Petr Vasiliev, 2018-08-01
@olafars

In encoding. Office only wants 1251. With others, you have to manually set the file. You feed him utf8

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question