Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.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 - Fatal编程技术网

Python 在文本文件中搜索多个字符串,如果退出,则删除一个文件

Python 在文本文件中搜索多个字符串,如果退出,则删除一个文件,python,Python,我有一个example.txt,它包含如下十六进制数据 09 06 07 04 00 00 01 00 1d 03 4b 2c a1 2a 02 01 b7 09 01 47 30 12 a0 0a 80 08 33 04 03 92 22 14 07 f0 a1 0b 80 00 81 00 84 01 00 86 00 85 00 83 07 91 94 71 06 00 07 19 09 06 07 04 r0 00 01 00 1d 03 4b 2c a1 2a 02

我有一个example.txt,它包含如下十六进制数据

09 06 07 04 00 00 01 00 1d 03 4b 2c a1 2a 02 01   
b7 09 01 47 30 12 a0 0a 80 08 33 04 03 92 22 14   
07 f0 a1 0b 80 00 81 00 84 01 00 86 00 85 00 83   
07 91 94 71 06 00 07 19

09 06 07 04 r0 00 01 00 1d 03 4b 2c a1 2a 02 01   
b7 09 01 47 30 1s a0 0a 80 08 33 04 03 92 22 14   
07 f0 a1 0b 80 00 81 0d 84 01 00 86 00 85 00 83   
07 91 94 71 06 

09 06 07 04 r0 00 01 00 1d 03 4b 2c a1 2a 02 01   
b7 09 01 47 30 1s a0 0a 80 08 33 04 03 92 22 14   
07 f0 a1 0b 80 00 81 0d 84 01 00 86 00 85 00 83   
b7 09 01 47 30 1s a0 0a 80 08 33 04 03 92 22 14

b7 09 01 47 30 1s a0 0a 80 08 33 04 03 92 22 14   
07 f0 a1 0b 80 00 81 0d 84 01 00 86 00 85 
我要做的是查找一个特定的字符串,如果退出,则继续在该点查找另一个字符串,依此类推。一旦我知道模式存在,那么我想删除一个文件,如果该模式不存在,则删除另一个文件

我的代码是:

import os

with open('example.txt') as file:
    if '12' in file.read():
        if ('80' or '25' or 'a6' or '1b') in file.read():
            if '04' in file.read():
                if '07' in file.read():
                    command1 = 'rm -f file2.json'
                    os.system(command1)
     else:
     command2 = 'rm -f file1.json'
     os.system(command2)

可以使用正则表达式(regex)在文件中查找具有此结构的所有组

import os
import re

file_path = "example.txt"
delete_file_path = "delete_me"
delete_file_ending = ".txt"
pattern = re.compile("12.*(?=[90|25|30]).*(?=40).*(?=20)")  # add a proper regex here to match all you required strings properly

with open(file_path) as file:
    text = file.read()
paragraphs = text.split(os.linesep)
paragraph_tokens = [re.findall(pattern, paragraph) for paragraph in paragraphs]

for i in range(paragraph_tokens):
    if paragraph_tokens[i]:
        os.remove(delete_file_path +s tr(i) + delete_file_ending)

如果您只想知道其中是否有任何匹配的模式,那么您也可以获得re.match,但由于re.match返回一个对象,因此您需要稍微更改if条件。

欢迎使用StackOveflow。网站上的问题需要明确。要求贡献者为您设计一个程序不是Stackoverflow的目的。你能把你的问题重新定义为一个具体的问题吗?看看如何为Stackoverflow提出一个好问题的指导。@Chris这似乎是一个非常直截了当的问题,包括他已经尝试过的代码。是的,经过思考,同意了。我在这里没有看到问题。。。所提供的代码是否有问题?如果是,请提供详细信息。如果不是的话——这更适合于它是一个新的版本来向一个问我的人展示我在做什么。现在已经解决了这看起来很棒,但是如何在re.compile()中添加几个字符串呢。我可以这样做吗<代码>重新编译(“12”和(“80”或“25”)因为我的想法是一旦“12”退出,就开始搜索“80”或“25”。这是正则表达式的一个教程:在你的例子中,你要么捕获所有像“[12 | 80 | 25]这样的标记,然后再执行逻辑(这在你的例子中可能没有多大帮助)或者,您可以使用此处描述的“向前看”或“向后看”功能创建一个组。在本例中,我强烈建议您导入regex,而不是导入re,因为regex库也支持默认re库不支持的“向前看”。我认为这样做的方法就像在遇到空白时建立断点但我不知道如何实现。有什么想法吗?@SemmelI添加了此功能。如果出现任何其他问题(例如,如果文件大于可用ram,请逐行读取…),请提出一个单独的问题,因为最初的问题现在有望得到正确的回答:)