通过查看某些字符并判断在何处分隔来尝试分隔行
Trying to separate lines by looking at certain characters and judging where to separate
使用Python 3.
所以基本上我有一段代码试图查看文本文件并查找小于号。如果它在它后面找到一个大于号,然后是一个大写字母,它就知道这是一个新行的开头,所以它会在那里放置一个 \n
。我 运行 陷入 "local variable" 错误,但我不知道为什么。它不应该发生,因为我在函数内部使用变量而不使用任何全局变量。奇怪的是,该代码适用于 'separate' 函数的前三个调用,但不适用于第四个。我唯一的解释是 while (example[x] != "<"):
循环由于某种原因在 'separate' 函数的第四次调用时根本没有执行。
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]
我连续四次使用该函数只是为了测试代码。我希望能够简单地循环该函数并自动处理 "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
我对 Python 和一般编码还很陌生,所以我知道我的代码非常糟糕和混乱。我知道我在这里做错了一些事情,所以请纠正我。
当第一个条件 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))
使用Python 3.
所以基本上我有一段代码试图查看文本文件并查找小于号。如果它在它后面找到一个大于号,然后是一个大写字母,它就知道这是一个新行的开头,所以它会在那里放置一个 \n
。我 运行 陷入 "local variable" 错误,但我不知道为什么。它不应该发生,因为我在函数内部使用变量而不使用任何全局变量。奇怪的是,该代码适用于 'separate' 函数的前三个调用,但不适用于第四个。我唯一的解释是 while (example[x] != "<"):
循环由于某种原因在 'separate' 函数的第四次调用时根本没有执行。
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]
我连续四次使用该函数只是为了测试代码。我希望能够简单地循环该函数并自动处理 "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
我对 Python 和一般编码还很陌生,所以我知道我的代码非常糟糕和混乱。我知道我在这里做错了一些事情,所以请纠正我。
当第一个条件 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))