由于限制,我无法使用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 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()
我能够创建一个脚本来编写所有文本并插入标记,这些标记对于插入的每组文本都是单独的,但是我不再知道在删除该部分时要做什么标记之间。
答案 0 :(得分:1)
这个答案的学分是lazy1和他的评论解决方案:
fL是一个行列表。和fL.index将罚款不属于它的一行。 您是否需要删除每一行或整个文件?在任何 case,使用字符串索引查找开始和结束然后切片。 类似于:i = line.find(DelStart)j = line.find(DelEnd)print line [:i] + line [j + len(DelEnd):]
# These Are the Variables for our file path and the start and ending points of what I want to delete.
#----------------------------------------------------------------------------------------------------
DelStart = "//StartBB" #Replace This With Proper Start Que
DelEnd = "//EndBB" #Replace this with the proper End Que
FilePath = "FILEPATH GOES HERE"
#----------------------------------------------------------------------------------------------------
#Sets Line to open our filepath and read it
line = open(FilePath).read()
#i finds our start place j finds our ending place
i = line.find(DelStart)
j = line.find(DelEnd)
#sets text as finding start and end and deleting them + everything inbetween
text = line[:i] + line[j+len(DelEnd):]
#Creates our out file and writes it with our filter removed
out = open(FilePath + '.out', 'w')
out.write(text)
#Closes Our New File
out.close()
#Ends Script
Script.close()
答案 1 :(得分:1)
虽然你的自我回答中的代码看起来应该有用,但它并不是特别优雅:
with
块来处理文件。line
被错误命名:它根本不包含一行,而是包含输入文件的全部内容。这是另一种选择:
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 open(...)
(向下滚动至开始的段落&#34;最好使用with关键字...&#34;)以确保即使您正确关闭文件遇到某种错误,以及一次读取和写入一行以节省内存。
如果您的文件 example.txt ,则包含以下内容:
one
two
three
//StartBB
four
five
six
//EndBB
seven
eight
nine
...然后按如下方式调用excise()
......
excise("example.txt", "//StartBB", "//EndBB")
...会做你想做的事:
<强> example.txt.out 强>
one
two
three
seven
eight
nine
答案 2 :(得分:0)
这仅使用内置字符串函数:
string = "This is the string"
stringA = "This"
stringB = " string"
stringC = stringA + stringB
stringD = stringC.replace(string, stringC)
答案 3 :(得分:0)
重写@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):]