TheChosenWong
TheChosenWong

Reputation: 1

Referencing a variable after separating it from a list

input = "12345_usa_wool_10x10_100_80.jpg"

def CleanData(input):
    data = input.split('_')

    sku = data[0]
    country = data[1].capitalize()
    material = data[2].capitalize()
    size = data[3]
    retail_price = data[4]
    sale_price = data[5]

CleanData(input)
print (sku)
print (country)

I'm getting

NameError: name 'sku' is not defined

I tried to store all the values in the list I created to easily reference back to them later.

Such as if they wanted total savings I could then later make something like

def Saving()
    total_saving = retail_price-sale_price
    return total_saving

So I can later have a final output of something like:

print (sku+" "+country+" "+sale_price+" "+Saving())

I'm a beginner and self-learner so I figured this isn't too hard of a problem but I don't know how to have sku,country,material, etc. be able to be referenced publicly.

Upvotes: 0

Views: 46

Answers (2)

cdarke
cdarke

Reputation: 44434

What you have is python scope, sku, and all the other variables assigned in the function, are local to the function. If you want them to be global, then mark them as such:

def CleanData(input):
    data = input.split('_')

    global sku
    sku = data[0]

.... and so on

However, it is generally not a good idea to use global variables in this way. We want our functions to be encapsulated so that they can be used in many programs. The problem with using globals is that you have to know that the name is not going to be used for something else in the program. If you need to reuse the code across programs then that will break.

The solution is to return some sort of container. For example it could be a dictionary, a list, or a tuple. Here is an example using a tuple:

def CleanData(input):
    data = input.split('_')

    # sku, country, material, size, retail_price, sale_price 
    return (data[0], 
            data[1].capitalize(),
            data[2].capitalize(),
            data[3],
            data[4],
            data[5])

sku, country, material, size, retail_price, sale_price = CleanData(input)
print (sku)
print (country)

You could probably simplify this further by just returning data. In addition you might wish to test len(data) to ensure you have the correct number of fields.

Upvotes: 1

rlbond
rlbond

Reputation: 67847

sku is defined in the CleanData function only, it does not have scope outside of that function.

I'd recommend using a dict object instead. E.g.,

def parseData(input):
    data = input.split('_')
    d = {}
    d['sku'] = data[0]
    d['country'] = data[1].capitalize()
    d['material'] = data[2].capitalize()
    d['size'] = data[3]
    d['retail_price'] = data[4]
    d['sale_price'] = data[5]
    return d

myData = parseData(input)
print(myData['sku'])
print(myData['country'])

You can also directly construct the dict:

def parseData(input):
    data = input.split('_')
    d = {'sku': data[0],
         'country': data[1].capitalize(),
         'material': data[2].capitalize(),
         'size': data[3],
         'retail_price': data[4],
         'sale_price': data[5]}
    return d

Upvotes: 2

Related Questions