Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.4k views
in Technique[技术] by (71.8m 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:

"definida_pelo_usuario":"N","descricao":"Transferu00eancia","descricao_padrao":"Transferu00eancia"

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:
        try:

            # 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

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...