2017-06-14 48 views
1

使用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和一般的编码,所以我知道我的代码是非常糟糕和混乱。我知道我在这里犯了严重的错误,所以请纠正我。

回答

0

当第一个条件while (example[x] != "<")为假且循环从不运行时,会出现问题,则不会执行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))