Syntax error no print [closed]

-3

code:

# -*- coding: utf-8 -*-
import MySQLdb

db = MySQLdb.connect(host="xxx.xxx.x.xx", user="xxxxx", passwd="xxxxxxxx", db="xxxxxxx")


cursor = db.cursor()

cursor.execute("SELECT 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.utentes ON centrodb.utentes.codigoutente = centrodb.RegistoConsultas.Utente LEFT OUTER JOIN centrodb.DiasSemana ON centrodb.DiasSemana.Id = centrodb.RegistoConsultas.DiaSemana")

myresult = cursor.fetchall()

for linha in myresult:
                dia = linha['Dias']
                hora = linha['HoraConsulta']
                saida = linha['HoraSaida']
                utente = linha['nome']
                consulta = linha['Consulta']
                local = linha['Descricao']
                contato =linha['Contato']

print​(​"Dia:", dia)
print​(​"Hora Consulta:", hora)
print​(​"Hora Saída:", saida)
print​(​"Utente:", utente)
print​(​"Consulta:", consulta)
print​(​"Local:", local)
print​(​"Contato:", contato)

The syntax error is in the first line of print. In the terminal the error is:

  

SyntaxError: invalid syntax

what should be displayed on the prints:

  • Day: Wednesday
  • time: 11:00 AM
  • Exit: 10:15:00
  • user: xxxxxxxxxx
  • query: Dentist
  • Location: Hospital São João
  • contact: xxxxxxx

I'll just show you why it's not a query problem:

# -*- coding: utf-8 -*-
    import MySQLdb

db = MySQLdb.connect(host="xxx.xxx.x.xx", user="xxxxx", passwd="xxxxxxxx", db="xxxxxxx")


cursor = db.cursor()

cursor.execute("SELECT 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.utentes ON centrodb.utentes.codigoutente = centrodb.RegistoConsultas.Utente LEFT OUTER JOIN centrodb.DiasSemana ON centrodb.DiasSemana.Id = centrodb.RegistoConsultas.DiaSemana")

myresult = cursor.fetchall()

print(myresult)

This prints the result in the table

    
asked by anonymous 07.12.2018 / 10:00

1 answer

2

Use a suitable text editor for programming

When copying your code and editing it for the example below, I discovered that there are invisible, non-printable characters between ( and " ) Checking here, I saw that it is the \u200b name "ZERO WIDTH SPACE". That is, your text editor has put a crazy, unprintable and invisible character in random positions in your code.

I was finalizing my initial response - and I give her tips on the publisher. But I thought you used the "notepad" which is pretty bad - word processors such as Word, OpenOffice, wordpad, are more likely what you used there - these tools are absolutely inappropriate > to edit code. There are hundreds of programming editors, dozens of very good and free quality, including one that comes with Python itself.

original response

Apart from what was said in the comments - the syntax of this script seems to be correct, and it would work in both Python 2 and Python 3

BUT - if you are saving the file with the default coding of Windows - which is Latin-1 (also known as cp-1252), because of the first line of the file: # -*- coding: utf-8 -*- which causes Python to read the file like utf-8, it will give syntax error on line print​(​"Hora Saída:", saida) - on account of the accented character.

The actual encoding of the file is done by the editor you use to edit the program. You should have the option to select the encoding. Without this, the accented% wizard will be written to your .py file as a byte that is invalid in utf-8.

It would help if you pasted the whole error instead of just the first word of the error message. (if it is, it will say that it is a "unicode decode error")

You do not mention the Python version - hopefully it's Python 3- It does not make sense to work with Python 2 today on new projects. You can simply remove the first line with the encoding indication - but try to save your file as utf-8 the same way. Tip: The "notepad" that comes with Windows is not a tool in which it is possible to write Python code - in addition to the coding problem, it does not have the automatic indentation feature.

Next to the installation of Python in Windows is installed í - it is an interactive environment and simple IDE, however quite complete, that will work well for this.

In addition to the coding issue and using Python 3: the recommandation for Python indentation is 4 spaces - the language syntax leaves free how you use indentation, since in each block it is maintained - so it does not give an error with its identation of more than 10 spaces - but this type of identification has no theoretical or practical advantage, either to write, to read or to keep the code. (but it's a clue that you should have used a text editor without auto-indentation - since it uses 4, at most 8, spaces by default, and had to be manually putting spaces in each line)

And finally, when you pass the syntax error, you will probably want to put the "print" all within the idle loop to print all the records, not just the last one.

In fact, there is no need to do that variable assignment block - values are as usable directly inside the dictionary, as they are for "own" variables for each one - in which case you are only typing more .

Furthermore, strings in Python have a for method to which you can pass a direct dictionary - this allows you to write your formatting directly in a single print - and passing the data all at once. You can use the delimitation of strigns with three quotation marks format to have more than one line inside the string to be printed. (note that the """ is of the string, not of the print)

from textwrap import dedent

for linha in myresult:
    print​(dedent("""​
    Dia: {Dias}
    Hora Consulta: {HoraConsulta}
    Hora Saída: {HoraSaida}
    ....
    """.format(**linha)

How to check your crazy Python characters:

The Python language itself can be used to find invisible and control characters like the one in the code above. (It will depend on the ability to copy and paste these characters to your system and your terminal - Windows is not the best tool). But if in addition to seeing the codes you want to see the Unicode name of the characters you can do so - the text with the line I want to analyze I copied and pasted directly from your code above:

In [100]: for char in """print("Dia:", dia)""": 
     ...:     print(char, hex(ord(char)), unicodedata.name(char)) 
     ...:                                                                                                
p 0x70 LATIN SMALL LETTER P
r 0x72 LATIN SMALL LETTER R
i 0x69 LATIN SMALL LETTER I
n 0x6e LATIN SMALL LETTER N
t 0x74 LATIN SMALL LETTER T
 0x200b ZERO WIDTH SPACE
( 0x28 LEFT PARENTHESIS
 0x200b ZERO WIDTH SPACE
" 0x22 QUOTATION MARK
D 0x44 LATIN CAPITAL LETTER D
i 0x69 LATIN SMALL LETTER I
a 0x61 LATIN SMALL LETTER A
: 0x3a COLON
" 0x22 QUOTATION MARK
, 0x2c COMMA
  0x20 SPACE
d 0x64 LATIN SMALL LETTER D
i 0x69 LATIN SMALL LETTER I
a 0x61 LATIN SMALL LETTER A
) 0x29 RIGHT PARENTHESIS
    
07.12.2018 / 13:24