简体   繁体   English

Python:读取文件,直到行与二进制模式下的字符串匹配

[英]Python: Read a file until a line matches a string in binary mode

Ok, so i have seen the other questions but i run into a unique issue. 好的,所以我看到了其他问题,但我遇到了一个独特的问题。 I have to open the file in binary mode in order to read it (i don't really understand why, but it works). 我必须以二进制模式打开文件才能读取它(我真的不明白为什么,但它有效)。 and I can easily print out the lines of the file no problem. 我可以轻松打印出文件的行没问题。 But when I try to find a specific line using re.search , I have issues because I have a string pattern and byte objects. 但是当我尝试使用re.search查找特定行时,我遇到了问题,因为我有一个字符串模式和字节对象。 Here is what I have so far: 这是我到目前为止:

input_file = open(input_file_path,  'rb',  0)

for line in input_file:
    if re.search("enum " + enum_name,  line, 0):
        print("Found it")
        print(line)
        exit()

enum_name is a user input so I really need to know how I can use both a string and the variable in my search of a file opened in binary mode (or how to open this file not in binary mode, I get the can't have unbuffered text I/O error when not in binary mode). enum_name是一个用户输入所以我真的需要知道如何在搜索以二进制模式打开的文件时使用字符串和变量(或者如何以二进制模式打开此文件,我得到的不能有不处于二进制模式时的无缓冲文本I / O错误)。 I have tried making my pattern for the search binary but I don't know what to do with the variable when I do that. 我已经尝试为搜索二进制文件制作模式,但是当我这样做时,我不知道如何处理变量。

You need to use a byte string as the pattern in your regex, something like the following should work: 您需要使用字节字符串作为正则表达式中的模式,如下所示:

if re.search(b"enum " + enum_name.encode('utf-8'), line):
    ...

The enum_name.encode('utf-8') here is used to convert the user input to a bytes object, depending on your environment you may need to use a difference encoding. 这里的enum_name.encode('utf-8')用于将用户输入转换为bytes对象,具体取决于您可能需要使用差异编码的环境。

Note that if your regex is really this simple, then you can probably just use a substring search instead. 请注意,如果你的正则表达式真的很简单,那么你可能只需要使用子字符串搜索。

You don't need re. 你不需要重新。 Try 尝试

if "enum " + enum_name in line:

Reading with 'b' is mostly about line endings. 用'b'读书主要是关于行结尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM