Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 运行代码时发生UnboundLocalError,这毫无意义_Python_Python 3.x - Fatal编程技术网

Python 运行代码时发生UnboundLocalError,这毫无意义

Python 运行代码时发生UnboundLocalError,这毫无意义,python,python-3.x,Python,Python 3.x,有人能帮忙吗? 昨天,当我完成了这段代码并测试了我所有的可能性时,它成功了。但今天它根本不起作用,它只是产生了一个错误 我的代码: def parseCSV(string): results = [] current = '' quoting = False quoting = False for i in range(0, len(string)): currentletter = string[i] if cur

有人能帮忙吗? 昨天,当我完成了这段代码并测试了我所有的可能性时,它成功了。但今天它根本不起作用,它只是产生了一个错误

我的代码:

def parseCSV(string):

    results = []
    current = ''
    quoting = False
    quoting = False


    for i in range(0, len(string)):
        currentletter = string[i]

        if currentletter == '"':
            if quoted == True:
                if quoting == True:
                    current = current + currentletter
                    quoting = False 
                else:
                    quoting = True

            else:
                quoted = True
                quoting = False

        else:

            shouldCheck  = False

            if quoted == True:

                if quoting == True:
                    quoted = False
                    quoting = False

                    shouldCheck = True

                else:
                    current = current + currentletter

            else:
                shouldCheck = True

            if shouldCheck == True:
                if currentletter == ',':
                    results.append(current)
                    current = ''

                else:
                    current = current +  currentletter

    results.append(current)
    return results


# Main
#Split CSV
Result = parseCSV('a,"b,c",d')
print(Result)
错误:

UnboundLocalError: local variable 'quoted' referenced before assignment

有人能解释什么是UnboundLocalError吗?非常感谢

错误代码:错误是我设置了两次引用,而其中一个应该被引用

def parseCSV(string):

    results = []
    current = ''
    quoting = False
    quoting = False


    for i in range(0, len(string)):
        currentletter = string[i]

        if currentletter == '"':
            if quoted == True:
                if quoting == True:
                    current = current + currentletter
                    quoting = False 
                else:
                    quoting = True

            else:
                quoted = True
                quoting = False

        else:

            shouldCheck  = False

            if quoted == True:

                if quoting == True:
                    quoted = False
                    quoting = False

                    shouldCheck = True

                else:
                    current = current + currentletter

            else:
                shouldCheck = True

            if shouldCheck == True:
                if currentletter == ',':
                    results.append(current)
                    current = ''

                else:
                    current = current +  currentletter

    results.append(current)
    return results


# Main
#Split CSV
Result = parseCSV('a,"b,c",d')
print(Result)

有人能关上这个吗?谢谢

在使用if语句之前,您是否将“quoted”设置为某个值?也许你在控制台上设置了它,但忘记将其包含在主代码中。你可以删除你自己的问题。你写的,没有其他答案,所以你可以使用你的问题下的删除按钮删除它。