Problems with python utf8 encoding

Asked

Viewed 151 times

0

Error I get when running the script:

inserir a descrição da imagem aqui

I get this bug by its name ~ which returns from the mysql table. If the table ~ in the name does not give error, if you put the ~ get this error.

my script:

#! /usr/bin/env python
# coding: utf8
import MySQLdb
import pycurl
import base64
import json
import datetime

db = MySQLdb.connect(host="xxx.xxx.x.xx", user="xxxxxx", passwd="xxxxxx", db="xxxxxx", charset = 'utf8')

cursor = db.cursor()

cursor.execute("SELECT DataConsulta, Dias, HoraConsulta, HoraSaida, nome, Consulta, centrodb.LocalConsulta.Descricao, Contato FROM centrodb.RegistoConsultas LEFT OUTER JOIN centrodb.LocalConsulta ON centrodb.LocalConsulta.Id = centrodb.RegistoConsultas.`Local` LEFT OUTER JOIN centrodb.UtentesCons ON centrodb.UtentesCons.codigoutente = centrodb.RegistoConsultas.Utente LEFT OUTER JOIN centrodb.DiasSemana ON centrodb.DiasSemana.Id = centrodb.RegistoConsultas.DiaSemana")

myresult = cursor.fetchall()

for linha in myresult:
 DataConsulta = linha[0]
 Dias = linha[1]
 HoraConsulta = linha[2]
 HoraSaida = linha[3]
 nome = linha[4]
 Consulta = linha[5]
 Descricao = linha[6]
 Contato = linha[7]

 today = datetime.date.today()
 data = today + datetime.timedelta(days=7)

 if DataConsulta == data:
  if __name__ == "__main__":
   url ="https://dashboard.360nrs.com/api/rest/sms"
   usrPass = "xxxxx:xxxxxxxx"
   data = json.dumps({
   "to":[Contato],
   "from":"xxxxxxx",
   "message":"LAR. Consulta de " + Consulta + " em " + DataConsulta + " para " + nome + ". " + Descricao + ". Responda Sim/Não para o acompanhamento. ",
   "encoding":"gsm-pt"       
   })
   b64Val = base64.b64encode(usrPass)
   headers=["Accept:Application/json","Authorization:Basic %s"%b64Val]
   c = pycurl.Curl()
   c.setopt(pycurl.URL, url)
   c.setopt(pycurl.HTTPHEADER,headers)
   c.setopt(pycurl.POST, 1)
   c.setopt(pycurl.POSTFIELDS, data)
   c.setopt(pycurl.SSL_VERIFYHOST, 0)
   c.setopt(pycurl.SSL_VERIFYPEER, 0)
   c.perform()
   http_code = c.getinfo(pycurl.HTTP_CODE)
   print(http_code)
  • Your database has UTF-8 encoding?

  • @Anderson Carlos Woss the table is coded utf8 - utf8_unicode_ci

1 answer

1


Looks like you’re using the python 2; This python version automatically does the implicit conversion between bytes (that in python 2 is str) and unicode; This can be a serious problem for you as the json module asks unicode and you’re passing bytes.

One way to solve it is to work with Unicode everywhere:

data = json.dumps({
    u"to": [Contato],
    u"from": u"xxxxxxx",
    u"message": u"LAR. Consulta de {} em {} para {}. {}. Responda Sim/Não para o acompanhamento. ".format(
        Consulta, DataConsulta, nome, Descricao),
    u"encoding": u"gsm-pt"       
})

That one u before the strings tells python 2 that they must be objects unicode and not str

That way you’ll be passing Unicode to the json.dumps() and the same won’t give problem anymore.

  • in my Ubuntu I have version 2.7.12 and version 3.5.2. I must call version plus 3.5.2 at the beginning of the script?

  • @Beginner if you want to use python 3, just call the script using it: python3 /caminho/enviosms.py but not required, python 2 can be used, but the syntax is different

Browser other questions tagged

You are not signed in. Login or sign up in order to post.