Python Forum
How to solve this problem Python configuration?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to solve this problem Python configuration?
#1
This error.
SyntaxError: Non-UTF-8 code starting with '\xe3' in file D:\Projetos\M74Chat\M74Chat\config.py on line 4, but no encoding decared;
see https://peps.python.org/pep-0263/ for details

My code.
import psycopg2
connection made successfully, generates this error
# and cursor.execute('SELECT * FROM contact')

I don't believe the only solution to this problem is to put in all .py files on the first line: "# coding: utf-8", serious?
Reply
#2
Please post code and complete error message, including the traceback.

\x is the start of an escape sequence for a unicode character. This should be followed by two digits that define the unicode character. \xe3 is a lower case "a" with tilde. I have no problem using in a program like this:
print("\\xe3 is a lower-case a with a tilde \xe3")
Notice that I do not specify the encoding of the source file. I am running windows 10 and python 3.11. What are you running?
Reply
#3
Thanks, deanhystad.
I don't know exactly what happened after using
# coding = utf-8
I removed it from the code and it's working.

This my code:
import psycopg2

try:
  # Conexão ao banco de dados
  con = psycopg2.connect(dbname="*", user="*", password="*", host="*", port="*")
  # Criando um cursor para executar comandos SQL
  cursor = con.cursor()
  
connected to DB

  # Executando uma consulta SQL
  cursor.execute("SELECT * FROM contact")
  # Obtendo os resultados da consulta
  resultado = cursor.fetchall()
  # Imprimindo os resultados
  for linha in resultado:
    print(linha)
  # Fechando o cursor e a conexão
  cursor.close()
  con.close()
except Exception as e:
  print("Erro ao conectar ao banco de dados: ", e)
The message in oder post.
deanhystad write Mar-22-2024, 06:49 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#4
How do you edit your files? I've noticed it is easy to confuse windows into thinking a file is latin1
Quote:The message in oder post.
That was the entire error message? Nothing else?
Reply
#5
Aren't you using Python 2.7 instead of Python 3 ?

Also if you have a file encoded in some other encoding and you add # coding: utf8 at the top of the file, this may confuse the Python compiler.
« We can solve any problem by introducing an extra level of indirection »
Reply
#6
(Mar-22-2024, 06:52 PM)deanhystad Wrote: How do you edit your files? I've noticed it is easy to confuse windows into thinking a file is latin1
Quote:The message in oder post.
That was the entire error message? Nothing else?

check image in Attachment.

Attached Files

Thumbnail(s)
   
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Two Python problems to solve djzsp 1 401 Mar-17-2024, 01:18 AM
Last Post: deanhystad
  How to solve pancake flip with Python longmen 5 2,087 May-09-2022, 05:16 PM
Last Post: deanhystad
  How to solve problem 1181 of the URI with numpy. thiagohps 4 2,681 Mar-22-2021, 04:11 PM
Last Post: buran
Star Interesting Intro to python problem I can't solve. Honestworker 5 12,320 Mar-04-2021, 02:05 AM
Last Post: BashBedlam
  solve probability problem by python Dreammer 1 1,854 Dec-24-2020, 09:51 AM
Last Post: Larz60+
  Algorithm to solve a case of Travelling Salesman Problem Ale888 3 3,026 Dec-11-2018, 07:49 PM
Last Post: buran
  Cross word puzzle solve using python constraint library aliyark145 1 3,300 Nov-29-2018, 10:53 AM
Last Post: Larz60+
  how to solve this problem. IndexError: axis 2 out of bounds [0, 2) bedio 10 8,990 Nov-21-2017, 02:47 PM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020