python - 未绑定(bind)本地错误 : local variable 'X' referenced before assignment Python for simbols fixer

标签 python

我必须在 python 中为 LateX 做一个 simbols 修复程序 所以我做了这段代码,我不知道为什么会显示

Traceback (most recent call last):
  File "C:\Users\Stark's ProBook\Desktop\prueba.py", line 92, in <module>
    BracketFind()
  File "C:\Users\Stark's ProBook\Desktop\prueba.py", line 30, in BracketFind
    for h in archivo:
UnboundLocalError: local variable 'archivo' referenced before assignment

这是我的代码,适用于 Python 2.7

# -*- coding: cp1252 -*-

archivo = open("test.txt", "r")
texto = archivo.readlines()
archivo = "".join(texto)
Opening = 0
Closes = 0
print archivo      

def Count():
    return contador('{')-contador('}')

def contador(simbolo):
    conta = 0
    for h in archivo:
        if simbolo == h:
            conta= conta+1
    return conta

def Insert(string, index):
    return string[:index] + '.' + string[index:]

def Remove(string, index):
    return string[:index-1] + '.' + string[index+1:]

def BracketFind():

    Opening = 0
    Closes = 0
    for h in archivo:
        if '{' == h:
            Opening = Opening+1
        elif '}' ==h:
            Closes = Closes+1

    print "Abiertas ({) "+ str(Opening) + " Cerradas (}) "+str(Closes)
    Position = -1
    StartReplacing = False
    OpenPosition = -1
    while True:
        Position = -1
        StartReplacing = False
        OpenPosition = -1
        if Count() == 0:
            print "Revision exitosa!!, No existe ninguna { sin cerrar " +"Si tienes problemas de compilacion no es por esta razón. " + "Revisa tu codigo en busca de otro error"
            return True
        if contador('{') == 0 and contador('}')>0:
            break
        if contador('{') > 0 and contador('}') == 0:
            break
        for Character in archivo:
            Position = Position+1
            if Character == '{':
                OpenPosition = Position
                StartReplacing = True
            if StartReplacing:
                if Character == '}':
                    try:
                        archivo = Remove(archivo,OpenPosition)
                        archivo = Insert(archivo,OpenPosition)
                        archivo = Remove(archivo,Position)
                        archivo = Insert(archivo,Position)
                    except:
                        break
    iPos = -1
    iCount = -1
    iTarget = 0
    iType = 0

    if '{' in archivo:
        iTarget = archivo.rfind('{')
        print iTarget
        iType = 1
    elif '}' in archivo:
            iTarget = archivo.rfind('}')
            iType = 2
    if iTarget == -1:
        return True
    if iType == 1:
        print "La llave que abre (" , iTarget , ") quizas es inecesaria o parece no estar cerrada"
    elif iType == 2:
            print "La llave que cierra (" , iTarget , ") quizas es inecesaria o parece no estar cerrada"
    for Character in archivo:
        iPos = iPos+1
        if Character == '{' or Character == '}':
            iCount = iCount+1
            if(iCount == iTarget):
               print "La llave ",iCount , "Parece tener error"
    return True

print Count()
BracketFind()

您知道造成这种情况的原因吗? 我不明白如果在执行开始时它打印“archivo”,为什么要显示这个

最佳答案

这与 python 如何处理作用域有关。最初,archivo 是一个全局变量,定义在任何函数或类之外;它可以从任何范围访问。 BracketFind() 包括几个archivo 的定义,即'archivo = Remove(archivo,OpenPosition)'。这会导致 archivo 恢复到该功能的本地范围;您尝试引用的全局变量不再可访问。

解决此问题的最简单方法是在 BracketFind() 的开头附近添加“global archivo”行,但更可靠的解决方案是重新编写代码,使 archivo 不再是全局变量。

关于python - 未绑定(bind)本地错误 : local variable 'X' referenced before assignment Python for simbols fixer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29932707/

相关文章:

python - 在 requirements.txt 中打包所有依赖项

python - 如何在Python中删除日期中的异常值?

python - Matlab相当于Numpy广播?

python - 运行 OpenSSL 系统调用

python - 模块未找到错误 : No module named 'requests_html'

python - 如何将输入列表中的值插入数据库中的表?

python - 流图 - Python 中的可视化

python - 为什么 zip 返回元组?

python数组,内存消耗巨大

python - 使用 Maya python api - 如何将选定的对象名称更改为大写?