F
F
fogersp2018-06-05 15:42:08
Python
fogersp, 2018-06-05 15:42:08

Python: working with encodings?

Good day!
I rarely use Python, but it became necessary to run a simple REST API in order to collect data from the server.
So, almost every time I use Python for something, I have problems with encodings. In this case, KOI8-R.
The fact is that on the server from where we take the data - the base in KOI8-R. How to convert correctly from KOI8-R to UTF-8 in python? For example, I take the following data in json: {'data': [{'fio': 'é×ÁÎÏ× áÌÅËÓÁÎÄÒ ÷ÌÁÄÉÍÉÒÏ×ÉÞ', 'id': 23232, 'login': 'puhlik'}]} or in raw fio = "\u00eb\u00cf\u00da\u00cc\u00d1\u00ce\u00cb\u00cf \u00f2\u00cf\u00cd\u00c1\u00ce \u00e9\u00cf\u00d3\u00c9\u00c6\u00cf\u00d7\u00c9"
How do I process fio to get the normal "Ivanov Alexander Vladimirovich" in utf-8. Tried encode and decode - nothing works.

#!/usr/bin/env python3

from flask import Flask, request, jsonify
from flask_restful import Resource, Api
from sqlalchemy import create_engine
from json import dumps

db_connect = create_engine('mysql://root:[email protected]/my_database', encoding='koi8-r')
app = Flask(__name__)
api = Api(app)

class Users(Resource):
    def get(self, user_id):
        conn = db_connect.connect()
        query = conn.execute("SELECT id, login, fio FROM user WHERE id=%d "  %int(user_id))
        result = {'data': [dict(zip(tuple (query.keys()), i)) for i in query.cursor]}
        return jsonify(result)

api.add_resource(Users, '/users/<int:user_id>')

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8088, threaded=True, debug=True)

Customer:
from urllib import request
import json
from sys import argv

with request.urlopen("http://server.ru:8088/users/{0}".format(argv[1])) as url:
    data = json.loads(url.read().decode())
    print(data['data'][0]['fio'])

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
fogersp, 2018-06-05
@fogersp

It's decided. It turned out that the base was in latin1, and the data was written in koi8-r
encode('latin1').decode('koi8-r')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question