python 不会引用局部变量,即使它位于同一个函数中

问题描述 投票:0回答:1

这是我不久前正在使用的代码的一部分。我试图修改代码,直到我决定将其恢复。我不明白为什么我的当前和上一个不再被引用,即使它们位于同一函数内。

部分代码

def getSKU():
    global current, previous
    location = "C:\\Users\\Onboarding\\Downloads\\SKU Awaiting Approval"

    namePattern = re.compile(r'exportData')
    for filename in os.listdir(location):
    if namePattern.match(filename):
        full_path = os.path.join(location, filename)
        current = pan.read_excel(full_path)
        break
    current.to_csv("current", index=False)
    prev_pattern = re.compile(r"SKU Awaiting Approval")
    for filename in os.listdir(location):
    if prev_pattern.match(filename):
        full_path = os.path.join(location, filename)
        previous = pan.read_excel(full_path)
        break


      
pandas variables
1个回答
0
投票

试试这个:

def getSKU():
    global current, previous
    location = "C:\\Users\\Onboarding\\Downloads\\SKU Awaiting Approval"

    namePattern = re.compile(r'exportData')
    for filename in os.listdir(location):
        if namePattern.match(filename):
            full_path = os.path.join(location, filename)
            current = pan.read_excel(full_path)
            break
    current.to_csv("current", index=False)
    prev_pattern = re.compile(r"SKU Awaiting Approval")
    for filename in os.listdir(location):
        if prev_pattern.match(filename):
            full_path = os.path.join(location, filename)
            previous = pan.read_excel(full_path)
            break
© www.soinside.com 2019 - 2024. All rights reserved.