Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python:删除其他文件中特定字符串之间的字符串_Python_String_Find - Fatal编程技术网

python:删除其他文件中特定字符串之间的字符串

python:删除其他文件中特定字符串之间的字符串,python,string,find,Python,String,Find,我有一个txt文件,比如: first.txt 约翰尼,水管工,纽约;安娜^医生^华盛顿;凯特,佛罗里达州 然后,我在文件夹中有一个或多个output3*.txt文件,数据一直保存在该文件夹中: 哈哈,水管工布拉布拉 另一种可能是: 哈哈,医生布拉布拉;哈哈 如果output3*.txt文件中没有“exit”一词,则需要等待几秒钟,然后在每个文件中没有“exit”的haha和blabla之间搜索这些词(水管工医生admin),并从第一个txt文件中删除这些词 file_names3 = glob

我有一个txt文件,比如:

first.txt

约翰尼,水管工,纽约;安娜^医生^华盛顿;凯特,佛罗里达州

然后,我在文件夹中有一个或多个output3*.txt文件,数据一直保存在该文件夹中:

哈哈,水管工布拉布拉

另一种可能是:

哈哈,医生布拉布拉;哈哈

如果output3*.txt文件中没有“exit”一词,则需要等待几秒钟,然后在每个文件中没有“exit”的haha和blabla之间搜索这些词(水管工医生admin),并从第一个txt文件中删除这些词

file_names3 = glob.glob(pathtemp+"/output3*.txt")
abort_after = 1 * 5
start = time.time()
while True:
    if not file_names3:
        break
    delta = time.time() - start
    if delta >= abort_after:
        with open(path+"/"+statuses, "a") as statuses:
            statuses.write("-----------------\n ERRORS:\n\n-----------------\n")
            for file_name in file_names3:
                statuses.write("%s" % file_name + " - file not done: ")
                with open(file_name, 'r') as prenotf:
                    reader=prenotf.read()
                    for "haha" in reader:
                        finding=reader[reader.find("haha")+5:reader.find("blabla")]


                        statuses.write(finding)

        break
    time.sleep(3)
    for file_name in file_names3:
        with open(file_name, "r") as zz:
            if "exit" in zz.read():   #<<<--- test data
                file_names3.remove(file_name)
                print ("\n ############# List of files still Waiting to be done:\n")
                print (file_names3)
file_names3=glob.glob(路径温度+“/output3*.txt”)
在=1*5之后中止
开始=时间。时间()
尽管如此:
如果不是文件名3:
打破
delta=time.time()-开始
如果delta>=在以下时间后中止:
打开(路径+“/”+状态,“a”)作为状态:
statuses.write(“----------------\n错误:\n\n-----------\n”)
对于文件名称3中的文件名称:
状态。写入(“%s”%file\u name+“-文件未完成:”)
以prenotf形式打开(文件名为“r”):
reader=prenotf.read()
读者中的“哈哈”是:
finding=reader[reader.find(“哈哈”)+5:reader.find(“blabla”)]
状态。写入(查找)
打破
时间。睡眠(3)
对于文件名称3中的文件名称:
打开(文件名为“r”)作为zz:

如果在zz.read()中“exit”:#当您在遍历对象时更改对象时,会弄乱固有的位置指针。这个指针是绝对的。如果从文件中删除10个字符,则文件的其余部分将上移,但指针不变。这将跳过接下来的10个字符

您的逻辑分为两部分,然后:

  • 解析第一个文件时写入第二个文件。完成后,可以将新文件移到旧名称
  • 保持活动标志。当你点击时关闭它,哈哈当你点击blabla时重新打开它
它看起来像这样:

temp_file = open("tempfile.txt", 'w')

active = True
for line in <your input>:
    if "haha" in line:
        active = True
    elif "blabla" in line:
        active = False
    elif active
        temp_file.write(line)
temp_file=open(“tempfile.txt”,“w”)
活动=真
对于线路输入:
如果“哈哈”在一行:
活动=真
行中的“blabla”符号:
活动=错误
elif活动
临时文件写入(行)

您能将其应用到程序的当前逻辑中吗?

您正在从
文件名3
中删除项目,同时在
中迭代文件名3:
为什么即使在读取文件后仍保持打开状态?