import serial import re import MySQLdb dbName = "your_database_name" tblName = "consumption" uName = "your_username" pswd = "your_password" ser = serial.Serial( port='/dev/ttyUSB0', baudrate=57600, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS ) db=MySQLdb.connect(user=uName, passwd=pswd,db=dbName) c = db.cursor() prevWatts = 0 deltaT = 0 while 1: line="" line = ser.readline() #read a '\n' terminated line print line #prints the output so you can see it working m = re.search('.*[0]*([1-9][0-9]*).*',line) n = re.search('.*