#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0

    Newbie indentation issue


    G'day all,

    I'm new to python, coming from a Microsoft based (C# etc) programming background.

    I recently ventured over to the dark side of Linux and Raspberry Pi, and am using Python to grab some data from the serial port and put it into a MySQL database located on the Pi.

    I have an issue where my script won't compile, flagging the line with an error :

    if __name__== '__main__':

    The most recent error is :
    'IndentationError: unindent does not match any outer indentation level'

    If I tab that code block in (or) out, a different error appears :
    'IndentationError: expected an indented block'

    Any help would be greatly appreciated.

    Cheers,

    Melbfella.

    The code :
    Code:
    def main():
    
    	
    	ser=serial.Serial('/dev/ttyUSB0',9600)
    
    	while 1:
    			sLine=ser.readline()
    			sFirstChar=sLine[:1]
    			if sFirstChar=="@":
    				#It's the date and time.  Write it to the first line of the display
    				a=1
    			elif sFirstChar=="~":
    				print sLine
    				sSplit=sLine.split("|")
    				sTime=sSplit[0]
    				sSensorID=sSplit[1]
    				sValue=sSplit[2]
    				
    				db=MySQLdb.connect("localhost","aaa","bbb","test")
    				cur=db.cursor()
    				
    				sSql="INSERT INTO SensorData (MCMillis,MCDateTime,SensorID,SensorValue,CreateDateTime) "
    				sSql += "VALUES (" + sTime + ","
    				sSql += "'" + str(datetime.datetime.now()) + "'," + sSensorID + "," + sValue + ",'" + str(datetime.datetime.now()) + "')"		
    				#print sSql
    				cur.execute(sSql)
    				db.commit()
    			else:
    				#It's chatter from the com port - dump it into the LCD
    			
    		if __name__ == '__main__':
    			main()
    Last edited by Melbfella; March 28th, 2013 at 07:39 PM. Reason: Add code block
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,854
    Rep Power
    481
    Use spaces not tabs for indentation.
    Use an editor that does this for you. (emacs)
    This might be your program?
    Code:
    def main():
    
        ser=serial.Serial('/dev/ttyUSB0',9600)
    
        while 1:
            sLine=ser.readline()
            sFirstChar=sLine[:1]
            if sFirstChar=="@":
                #It's the date and time.  Write it to the first line of the display
                a=1
            elif sFirstChar=="~":
                print sLine
                sSplit=sLine.split("|")
                sTime=sSplit[0]
                sSensorID=sSplit[1]
                sValue=sSplit[2]
                db=MySQLdb.connect("localhost","aaa","bbb","test")
                cur=db.cursor()
                sSql="INSERT INTO SensorData (MCMillis,MCDateTime,SensorID,SensorValue,CreateDateTime) "
                sSql += "VALUES (" + sTime + ","
                sSql += "'" + str(datetime.datetime.now()) + "'," + sSensorID + "," + sValue + ",'" + str(datetime.datetime.now()) + "')"
                #print sSql
                cur.execute(sSql)
                db.commit()
            else:
                #It's chatter from the com port - dump it into the LCD
    
    
    if __name__ == '__main__':
        main()
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Use spaces not tabs for indentation.
    Use an editor that does this for you. (emacs)
    This might be your program?
    Code:
    def main():
    
        ser=serial.Serial('/dev/ttyUSB0',9600)
    
        while 1:
            sLine=ser.readline()
            sFirstChar=sLine[:1]
            if sFirstChar=="@":
                #It's the date and time.  Write it to the first line of the display
                a=1
            elif sFirstChar=="~":
                print sLine
                sSplit=sLine.split("|")
                sTime=sSplit[0]
                sSensorID=sSplit[1]
                sValue=sSplit[2]
                db=MySQLdb.connect("localhost","aaa","bbb","test")
                cur=db.cursor()
                sSql="INSERT INTO SensorData (MCMillis,MCDateTime,SensorID,SensorValue,CreateDateTime) "
                sSql += "VALUES (" + sTime + ","
                sSql += "'" + str(datetime.datetime.now()) + "'," + sSensorID + "," + sValue + ",'" + str(datetime.datetime.now()) + "')"
                #print sSql
                cur.execute(sSql)
                db.commit()
            else:
                #It's chatter from the com port - dump it into the LCD
    
    
    if __name__ == '__main__':
        main()
    Thanks for that. I have been cautious about using tabs not spaces, but went through the code again to make sure. There are definitely no spaces where tabs should be, and the error persists.

    I'm using Geany as the editor, which seems to deal with tabs nicely.
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,854
    Rep Power
    481
    Let's replace your tab characters with 8 vertical bars.
    It's obvious that
    if __name__ == '__main__':
    doesn't match the indentation of a prior level. Actually it's obvious without the replacement.

    Code:
    def main():
    
    ||||||||
    ||||||||ser=serial.Serial('/dev/ttyUSB0',9600)
    
    ||||||||while 1:
    ||||||||||||||||||||||||sLine=ser.readline()
    ||||||||||||||||||||||||sFirstChar=sLine[:1]
    ||||||||||||||||||||||||if sFirstChar=="@":
    ||||||||||||||||||||||||||||||||#It's the date and time.  Write it to the first line of the display
    ||||||||||||||||||||||||||||||||a=1
    ||||||||||||||||||||||||elif sFirstChar=="~":
    ||||||||||||||||||||||||||||||||print sLine
    ||||||||||||||||||||||||||||||||sSplit=sLine.split("|")
    ||||||||||||||||||||||||||||||||sTime=sSplit[0]
    ||||||||||||||||||||||||||||||||sSensorID=sSplit[1]
    ||||||||||||||||||||||||||||||||sValue=sSplit[2]
    ||||||||||||||||||||||||||||||||
    ||||||||||||||||||||||||||||||||db=MySQLdb.connect("localhost","aaa","bbb","test")
    ||||||||||||||||||||||||||||||||cur=db.cursor()
    ||||||||||||||||||||||||||||||||
    ||||||||||||||||||||||||||||||||sSql="INSERT INTO SensorData (MCMillis,MCDateTime,SensorID,SensorValue,CreateDateTime) "
    ||||||||||||||||||||||||||||||||sSql += "VALUES (" + sTime + ","
    ||||||||||||||||||||||||||||||||sSql += "'" + str(datetime.datetime.now()) + "'," + sSensorID + "," + sValue + ",'" + str(datetime.datetime.now()) + "')"||||||||||||||||
    ||||||||||||||||||||||||||||||||#print sSql
    ||||||||||||||||||||||||||||||||cur.execute(sSql)
    ||||||||||||||||||||||||||||||||db.commit()
    ||||||||||||||||||||||||else:
    ||||||||||||||||||||||||||||||||#It's chatter from the com port - dump it into the LCD
    ||||||||||||||||||||||||
    ||||||||||||||||if __name__ == '__main__':
    ||||||||||||||||||||||||main()
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    1
    Rep Power
    0
    That is a nice start to it.

IMN logo majestic logo threadwatch logo seochat tools logo