Skip to main content
 首页 » 编程设计

python之通过查看某些字符并判断在何处分隔来尝试分隔行

2025年02月15日7tuyile006

使用 Python 3。

基本上我有一段代码试图查看文本文件并查找小于号。如果它在它后面找到一个大于号,然后是一个大写字母,它就知道这是一个新行的开头,所以它会放置一个 \n。那里。我遇到了“局部变量”错误,但我不知道为什么。它不应该发生,因为我在函数内部使用变量而不使用任何全局变量。奇怪的是,该代码适用于“单独”函数的前三个调用,但不适用于第四个。我唯一的解释是 while (example[x] != "<"):无论出于何种原因,循环在第四次调用“单独”函数时根本没有执行。

example = "Tags for HTML: <!--...--> Defines a comment <!DOCTYPE> Defines the document 
type <a> Defines a hyperlink <abbr> Defines an abbreviation or an acronym <acronym> Not 
supported in HTML5. Use <abbr> instead. Defines an acronym <address> Defines contact  
information for the author/owner of a document <applet> Not supported in HTML5. Use 
<embed> or <object> instead. Defines an embedded applet <area> Defines an area inside 
an image-map <article> Defines an article <aside> Defines content aside from the page  
content <audio> Defines sound content <b> Defines bold text" 
 
x = 0 
def separate(x, example): 
    f=open('format_output.txt','w') 
 
    #looking for the first less than sign 
    while (example[x] != "<"): 
        x+=1 
        #advancing and storing the lineholder so it can enter a newline when done 
        lineholder = x 
 
    #looking for the first greater than sign 
    while (example[x] != ">"): 
        #advancing the cursor 
        x+=1 
 
    #checking if the position two characters from the cursor is an uppercase letter 
    this = example[x+2:x+3] 
    if(this.isupper()): 
        #if it is, print "it's upper" 
        print("its upper") 
        #putting everything before lineholder into a variable 
        temp_file_string = example[:lineholder] 
        #adding a newline to it 
        temp_file_string = temp_file_string + "\r\n" 
        #putting everything after linholder into another variable 
        rest_of_string = example[lineholder:] 
        #writing them combined into the output file 
        f.write(temp_file_string + rest_of_string) 
        #rewinding the file cursor so the file can be read and printed to the shell  
        f.seek(0) 
        f=open('format_output.txt','r') 
        example = f.read() 
        print("\n\nprinting contents:\n\n" + example) 
        f.close 
        return (x, example)             
    else: 
        #else say 'Isn't Uppper' 
        lineholder = x 
        print("Isn't Upper") 
        return (x , example) 
 
(x, example) = separate(x, example) 
(x, example) = separate(x, example) 
(x, example) = separate(x, example) 
(x, example) = separate(x, example) 
 
 
print('\n'+str(x)) 

错误信息指出:

local variable 'lineholder' referenced before assignment 

在线:

temp_file_string = example[:lineholder] 

我连续四次使用该函数只是为了测试代码。我希望能够简单地循环该函数并自动处理“示例”,在每个标记完成描述后放置一个换行符。

输出应该是:

Tags for HTML:  
<!--...--> Defines a comment  
<!DOCTYPE> Defines the document type  
<a> Defines a hyperlink  
<abbr> Defines an abbreviation or an acronym  
<acronym> Not supported in HTML5. Use <abbr> instead. Defines an acronym  
<address> Defines contact information for the author/owner of a document  
<applet> Not supported in HTML5. Use <embed> or <object> instead. Defines an embedded applet  
<area> Defines an area inside an image-map  
<article> Defines an article  
<aside> Defines content aside from the page content  
<audio> Defines sound content  
<b> Defines bold text 

我对 Python 和一般编码还很陌生,所以我知道我的代码非常糟糕和困惑。我知道我在这里做的事情严重错误,所以请纠正我。

请您参考如下方法:

问题出现在第一个条件while (example[x] != "<")为 false 并且循环永远不会运行,则 lineholder = x不执行。

所以你应该初始化lineholder = x之前的某个地方。

example = """Tags for HTML: <!--...--> Defines a comment <!DOCTYPE> Defines the document 
type <a> Defines a hyperlink <abbr> Defines an abbreviation or an acronym <acronym> Not 
supported in HTML5. Use <abbr> instead. Defines an acronym <address> Defines contact  
information for the author/owner of a document <applet> Not supported in HTML5. Use 
<embed> or <object> instead. Defines an embedded applet <area> Defines an area inside 
an image-map <article> Defines an article <aside> Defines content aside from the page  
content <audio> Defines sound content <b> Defines bold text""" 
 
x = 0 
def separate(x, example): 
    lineholder = x 
    f=open('format_output.txt','w') 
 
    #looking for the first less than sign 
    while (example[x] != "<"): 
        x+=1 
        #advancing and storing the lineholder so it can enter a newline when done 
        lineholder = x 
 
    #looking for the first greater than sign 
    while (example[x] != ">"): 
        #advancing the cursor 
        x+=1 
 
    #checking if the position two characters from the cursor is an uppercase letter 
    this = example[x+2:x+3] 
    if(this.isupper()): 
        #if it is, print "it's upper" 
        print("its upper") 
        #putting everything before lineholder into a variable 
        temp_file_string = example[:lineholder] 
        #adding a newline to it 
        temp_file_string = temp_file_string + "\r\n" 
        #putting everything after linholder into another variable 
        rest_of_string = example[lineholder:] 
        #writing them combined into the output file 
        f.write(temp_file_string + rest_of_string) 
        #rewinding the file cursor so the file can be read and printed to the shell  
        f.seek(0) 
        f=open('format_output.txt','r') 
        example = f.read() 
        print("\n\nprinting contents:\n\n" + example) 
        f.close 
        return (x, example)             
    else: 
        #else say 'Isn't Uppper' 
        lineholder = x 
        print("Isn't Upper") 
        return (x , example) 
 
(x, example) = separate(x, example) 
(x, example) = separate(x, example) 
(x, example) = separate(x, example) 
(x, example) = separate(x, example) 
 
 
print('\n'+str(x))