Hi guys,
Can anyone help me to fix my code so I don't get the unexpected unindent error?
The code is very big, so I'll only post a small part so you can help me.
I'm using the PyScripter IDE.

def pegar_dados_serial():
        ser = serial.Serial(port='COM4', baudrate=115200, timeout=2)
        #Rotina para leitura dos dados no receptor
        while True:
                try:
                    for i in ser.read():
                        lista.append(ord(i))
                        if k == 17:
                            k = 0
                            ValoresLidos['RSSI'] = lista[4]
                            ValoresLidos['TAG_ID'] = lista[9]
                            lista = []
                            print ValoresLidos
                            #Rotina para conexão com o DB e inserção dos dados na tabela
                            db = MySQLdb.connect(host="localhost",port=3306,user="root", passwd="xxxxxx",db="mydb")
                            c = db.cursor()
                            c.execute("""
                              INSERT INTO RFID (TAG_ID, RSSI)
                              VALUES
                              (lista[9], lista[4])
                                """)
                            print "Linhas inseridas: %d" % c.rowcount
                        k+=1

Thanks in advance.

Edited 6 Years Ago by victordq: n/a

It doesn't say anything specific. It only says "Unexpected Unindent" and highlights line 23 of the code above.
If I remove line 23, it'll point to line 22 and so on.

It does this to the entire code I wrote. I don't know what to do.

Have you tried indenting k+=1 one more time?

I'm sorry if I can't be of much help; new to Python and currently don't have Python on this computer I'm on.

where is except clause, you can have try without except (else and finally are optional)

Your indents are huge, use PEP8 recommendation of 4 spaces.

def pegar_dados_serial():
    ser = serial.Serial(port='COM4', baudrate=115200, timeout=2)
    #Rotina para leitura dos dados no receptor
    while True:
        try:
            for i in ser.read():
                lista.append(ord(i))
                if k == 17:
                    k = 0
                    ValoresLidos['RSSI'] = lista[4]
                    ValoresLidos['TAG_ID'] = lista[9]
                    lista = []
                    print ValoresLidos
                    #Rotina para conexão com o DB e inserção dos dados na tabela
                    db = MySQLdb.connect(host="localhost",port=3306,user="root", passwd="xxxxxx",db="mydb")
                    c = db.cursor()
                    c.execute("""
                    INSERT INTO RFID (TAG_ID, RSSI)
                    VALUES
                    (lista[9], lista[4])""")
                    print "Linhas inseridas: %d" % c.rowcount
                k+=1
        except ValueError as e:
            print e
            raise

Edited 6 Years Ago by pyTony: n/a

I've even tried retyping the whole thing to make sure nothing was wrong because I copied and pasted parts of the code.
When I retyped I tested it line by line and I start getting the error at line 8.

This article has been dead for over six months. Start a new discussion instead.