我有一个包含很多行的文件。每行以 {"id": 开头,后跟引号中的 id 编号。 (即{“id”:“106”)。我正在尝试使用正则表达式逐行搜索整个文档并打印与 5 个不同 id 值匹配的行。为此,我创建了一个包含 ids 的列表,并希望迭代该列表,仅匹配以 {"id": "(list 中的 id number)"开头的行。我真的很困惑如何做到这一点。这是我到目前为止所拥有的:
f= "bdata.txt"
statids = ["85", "106", "140", "172" , "337"]
x= re.findall('{"id":', statids, 'f')
for line in open(file):
print(x)
我不断收到的错误代码是:TypeError: unsupported operand type(s) for &: 'str' and 'int'
我需要匹配整行,以便我可以将其拆分并放入一个类中。
有什么建议吗?感谢您抽出时间。
最佳答案
您可以使用 regex 从该行检索 ID , ^\{\"id\":\"(\d+)\"
其中 group#1 的值将为您提供 id。然后,您可以检查 id 是否存在于 statids
中。
演示:
import re
statids = ["85", "106", "140", "172", "337"]
with open("bdata.txt") as file:
for line in file:
search = re.search('^\{\"id\": \"(\d+)\"', line)
if search:
id = search.group(1)
if id in statids:
print(line.rstrip())
对于文件中的以下示例内容:
{"id": "100" hello
{"id": "106" world
{"id": "2" hi
{"id": "85" bye
{"id": "10" ok
{"id": "140" good
{"id": "165" fine
{"id": "172" great
{"id": "337" morning
{"id": "16" evening
输出将是:
{"id": "106" world
{"id": "85" bye
{"id": "140" good
{"id": "172" great
{"id": "337" morning
关于使用正则表达式进行 Python 文件搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69637474/