0

I am trying to compare certain values with diff times in CSV file. ex: iam searching for the first 27 bytes value and save its time. Then second 27 value and time is different from first one then save its time then third etc.The problem is when i try to save time to a variable to compare it with the second same value it gives me this error :

UnboundLocalError: local variable 'T3' referenced before assignment "

import csv
import os
import glob


class dot(object):
    Time=""
    Payload=""
    Timediff=""

global T1
global T2
global T3
global T4

def searchfunc():
    path = '/Users/mohamedshaaban/Downloads/tes/Camera'
    for infile in glob.glob( os.path.join(path, '*csv') ):
        with open(infile) as inf:
        reader = csv.reader(inf)
        partname = partsize = None
        partname2 = partsize2 = None
        partname3 = partsize3 = None
        partname4 = partsize4 = None
        partname5 = partsize5 = None
        camera = dot()
        camera.Payload1='30 bytes'
        camera.Payload2='27 bytes'
        camera.Payload3='17 bytes'
        camera.Payload4='5 bytes'
        camera.Payload5='6 bytes'

        for row in reader:
            if camera.Payload1 in row[2] and partname is None:
                x = row[2]
                partname = x[0:-1]
                T1 = float(row[4])
            if camera.Payload2 in row[2] and partname2 is None:
                x2 = row[2]
                partname2 = x2[0:-1]
                T2 = float(row[4])
                #print partname2
                #print Time2
            if camera.Payload2 in row[2] and partname3 is None: 
                if float(row[4]) != T2:
                    T3 = float(row[4])
                    x3 = row[2]
                    partname4 = x3[0:-1]
                    print partname3
            if camera.Payload2 in row[2] and partname4 is None:
                if float(row[4]) != T3:
                    T4 = float(row[4])
                    x4 = row[2]
                    partname4 = x4[0:-1]
                    print partname3
                    print Time5             




searchfunc()

2 Answers2

1

You're using globals incorrectly - the global modifier is used inside of a function to indicate that the variable has global read/write permissions. However, T3 is never set on certain execution paths, and so you get this error.

You want your setup to be something of the form:

T1 = None # or your preferred initial value
T2 = None
T3 = None
T4 = None

def searchfunc():
    global T1, T2, T3, T4

Note, however, that you should avoid using globals when you can. Globals are inherently bad programming style and practice. There are some fantastic links in this answer to "Why are global variables evil?".

Community
  • 1
  • 1
Rushy Panchal
  • 16,979
  • 16
  • 61
  • 94
  • Thanks Rushy it fix the error but did not work as i wanted to. the output now is :2.71373925 None 2.776665375 None 2.776665375 .................... what i want it is the payload and the time for it only. For ex: 27 bytes (3C 1F 6B 04 D6 71 9F BD CF 7B 27 38 45 A5 8F 53 B4 1D 0C 07 6A FB AF 4D 0B 46 BF) 2.207133125 – Shaa'ban Mego May 01 '16 at 18:47
0

You need to declare the vars as global inside your function. Otherwise python assumes it's in the local scope only.

Try adding this under your function declaration (leaving the vars declared in the outer scope as well:

def searchfunc():
    global T1, T2, T3, T4
tknickman
  • 4,285
  • 3
  • 34
  • 47