Python 删除文件中两个字符串之间的所有文本,而不导入任何模块
由于限制,我不能使用任何导入模块,如re、sys或os等。 我一开始要进口的东西都卖完了。这让我的生活陷入了地狱。 以下是我到目前为止得到的信息:Python 删除文件中两个字符串之间的所有文本,而不导入任何模块,python,string,python-2.7,text-processing,Python,String,Python 2.7,Text Processing,由于限制,我不能使用任何导入模块,如re、sys或os等。 我一开始要进口的东西都卖完了。这让我的生活陷入了地狱。 以下是我到目前为止得到的信息: #I CANT USE ANY IMPORT MODULES DUE TO RESTRICTIONS ON THE MACHINE THIS RUNS ON :( # # These Are the Variables for our file path and the start and ending points of what I want t
#I CANT USE ANY IMPORT MODULES DUE TO RESTRICTIONS ON THE MACHINE THIS RUNS ON :(
#
# These Are the Variables for our file path and the start and ending points of what I want to delete.
#----------------------------------------------------------------------------------------------------
DelStart = "//StartBB"
DelEnd = "//EndBB"
FilePath = "C:\\Users\\kenne_000\\Desktop\\New folder\\test.sqf"
#----------------------------------------------------------------------------------------------------
#Opens the FilePath in Read set this command to f
f = open(FilePath).read()
#Sets value of out to create file in Write mode called "Filepath".out
Out = open(FilePath + '.out', 'w')
#assigns fL as open FilePath and split it up line by line for indexing
fL = f.split('/n')
#Assings LN as index our file and find the lines DelStart and DelEnd
LN = fL.index(DelStart,DelEnd)
#This Gives an error
#>>> LN = fL.index(DelStart,DelEnd)
#Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
#TypeError: slice indices must be integers or None or have an __index__ method
#text is replacing everything between our start and endpoints with nothing ""
# STILL NEED THIS SECTION NO CLUE HOW TO DO THIS
#Writes our new file Minus our designated start and end points
out.write(text)
#Closes Our New File
out.close()
#Ends Script
Script.close()
我能够创建一个脚本来编写所有文本并插入标记,这些标记对于插入的每一组文本都是独立的,但我不再知道在删除标记之间的部分时该怎么做。这只使用内置的字符串函数:
string = "This is the string"
stringA = "This"
stringB = " string"
stringC = stringA + stringB
stringD = stringC.replace(string, stringC)
这仅使用内置字符串函数:
string = "This is the string"
stringA = "This"
stringB = " string"
stringC = stringA + stringB
stringD = stringC.replace(string, stringC)
这个答案的功劳归于lazy1和他的评论解决方案: fL是一个行列表。而fL.index将细化一条线,而不是其中的一部分。 您需要删除每一行还是整个文件?无论如何 在这种情况下,使用字符串索引查找开始和结束,然后切片。 类似于:i=line.findDelStart j=line.findDelEnd print 行[:i]+行[j+lenDelEnd:]
这个答案的功劳归于lazy1和他的评论解决方案: fL是一个行列表。而fL.index将细化一条线,而不是其中的一部分。 您需要删除每一行还是整个文件?无论如何 在这种情况下,使用字符串索引查找开始和结束,然后切片。 类似于:i=line.findDelStart j=line.findDelEnd print 行[:i]+行[j+lenDelEnd:]
虽然您的自我回答中的代码看起来应该有效,但它并不特别优雅: 您真的应该使用with块来处理文件。 变量行命名错误:它根本不包含一行,而是包含输入文件的全部内容。 您的脚本一次读取整个文件,这对于大型文件在内存使用方面可能会有问题。 这里有一个替代方案:
def excise(filename, start, end):
with open(filename) as infile, open(filename + ".out", "w") as outfile:
for line in infile:
if line.strip() == start:
break
outfile.write(line)
for line in infile:
if line.strip() == end:
break
for line in infile:
outfile.write(line)
此函数使用向下滚动到段落开头。最好使用with关键字。。。确保即使遇到某种错误,文件也能正确关闭,以及一次读写一行以节省内存
如果您有一个包含以下内容的example.txt文件:
one
two
three
//StartBB
four
five
six
//EndBB
seven
eight
nine
。。。然后调用消费税如下
excise("example.txt", "//StartBB", "//EndBB")
。。。将执行您想要的操作:
example.txt.out
虽然您的自我回答中的代码看起来应该有效,但它并不特别优雅: 您真的应该使用with块来处理文件。 变量行命名错误:它根本不包含一行,而是包含输入文件的全部内容。 您的脚本一次读取整个文件,这对于大型文件在内存使用方面可能会有问题。 这里有一个替代方案:
def excise(filename, start, end):
with open(filename) as infile, open(filename + ".out", "w") as outfile:
for line in infile:
if line.strip() == start:
break
outfile.write(line)
for line in infile:
if line.strip() == end:
break
for line in infile:
outfile.write(line)
此函数使用向下滚动到段落开头。最好使用with关键字。。。确保即使遇到某种错误,文件也能正确关闭,以及一次读写一行以节省内存
如果您有一个包含以下内容的example.txt文件:
one
two
three
//StartBB
four
five
six
//EndBB
seven
eight
nine
。。。然后调用消费税如下
excise("example.txt", "//StartBB", "//EndBB")
。。。将执行您想要的操作:
example.txt.out
将@KennethDWhite答案的重要部分重写为函数。我只想把它作为对他的回答的评论发布,但格式不正确
# ToDo: this also deletes the delimiters. Perhaps add a parameeter to indicuate
# whether to do so,or just to delete the text between them?
def DeleteStringBetween(string, startDelimiter, endDelimiter):
startPos = string.find(startDelimiter)
endPos = string.find(endDelimiter)
# If either delimiter was not found, return the string unchanged.
if (startPos == -1) or (endPos == -1):
return string
return string[:startPos] + string[endPos + len(endDelimiter):]
将@KennethDWhite答案的重要部分重写为函数。我只想把它作为对他的回答的评论发布,但格式不正确
# ToDo: this also deletes the delimiters. Perhaps add a parameeter to indicuate
# whether to do so,or just to delete the text between them?
def DeleteStringBetween(string, startDelimiter, endDelimiter):
startPos = string.find(startDelimiter)
endPos = string.find(endDelimiter)
# If either delimiter was not found, return the string unchanged.
if (startPos == -1) or (endPos == -1):
return string
return string[:startPos] + string[endPos + len(endDelimiter):]
fL是一个行列表。而fL.index将细化一条线,而不是其中的一部分。您需要删除每一行还是整个文件?在任何情况下,使用字符串索引查找开始和结束,然后切片。类似于:i=line.findDelStart j=line.findDelEnd print line[:i]+line[j+lenDelEnd:]您是否有机会在代码片段中向我展示这一点?另外,整行是//StartBB然后下一行文本开始,然后在其自身行的结尾是//EndBB NM我明白您的意思了xd最后一部分是代码示例,由于某些原因,格式不正确。是的,你必须这样做,因为答案代码dosnt格式在commentsfL中是一个行列表。而fL.index将细化一条线,而不是其中的一部分。您需要删除每一行还是整个文件?在任何情况下,使用字符串索引查找开始和结束,然后切片。类似于:i=line.findDelStart j=line.findDelEnd print line[:i]+line[j+lenDelEnd:]您是否有机会在代码片段中向我展示这一点?另外,整行是//StartBB然后下一行文本开始,然后在其自身行的结尾是//EndBB NM我明白您的意思了xd最后一部分是代码示例,由于某些原因,格式不正确。是的,您必须在注释中以答案代码dosnt格式执行此操作。但是,目标是删除从//StartBB到//EndBB的所有文本等跨越多行的所有内容,以便在脚本完成后,所有内容都消失。不过,目标是删除//StartBB中的所有内容文本等跨多个 LE行一直到//EndBB,这样当脚本完成时,所有这些都消失了。