Welcome to Qi-U Community for programmer and developer-Open, Learning and Share


0 votes
in Technique[技术] by (31.9m points)

python - How to convert API sourced String Unicode to UTF-8

I'm having trouble converting data from the following list into UTF-8, I have to convert it in order to insert it into a SQL table using pyodbc:

cod_cat = ['Transfer\u00eancia', 'Entrada de Transfer\u00eancia', 'Sa\u00edda de Transfer\u00eancia']

The same data when I try it on postman:

cod_cat = ['Transferência', 'Entrada de Transferência','Saída de Transferência']

This list comes from an API request using the following code:

res = requests.post(url, json=teste)
resposta = res.text

I've used an function that finds specific delimiters in the request response and adds the string in between these delimiters everytime they show up.

I've tried to:

for i in lista_cat:
     i.decode('utf-8', 'ignore')

and, before the library imports:

# -*- coding: utf-8 -*

Also I tried to encode() and then decoding again, however all three didn't get me proper results, I've tried to find out which encoding type "res" is getting me but as I found in other questions

Correctly detecting the encoding all times is impossible.

My guess is that the .text isn't the best way to get these special characters data, however I don't know any other way around that.

Here is a sample of what res.text looks like:


The function I used in order to make the list:

resposta = res.text
def achar_str(inicio, delimitante):
    foo = resposta
    bar = inicio
    lista = []
    idx = 0
    ind = 0
    countador = 0
    while True:

            # find "foo"
            found_at = foo.index(bar, idx)
            achar = foo.index(delimitante, ind)

            # move the index to after "bar"
            idx = found_at + len(bar)
            ind = achar + len(produto)

            # append with strings between delimiters
            lista.append(res.text[found_at + len(bar):achar])
            # contador
            countador += 1

        except ValueError:
            return lista

Welcome To Ask or Share your Answers For Others

Please log in or register to answer this question.

1 Answer

0 votes
by (31.9m points)

Welcome to Qi-U Community for programmer and developer-Open, Learning and Share