gpt4 book ai didi

python - 匹配具有特定字符串的行以提取值 Python Regex

转载 作者:太空宇宙 更新时间:2023-11-04 07:50:00 24 4
gpt4 key购买 nike

我在为这个任务找到正确的正则表达式时遇到了一些问题,请原谅我的初学者技能。我想做的只是从“可用”的行中获取 id 值:true 而不是“可用”:false。我能够通过 re.findall('"id":(\d{13})', line, re.DOTALL) 获取所有行的 ID(13 正好匹配 13 位数字,因为代码中还有其他 id 少于 13 位,我不需要)。

{"id":1351572979731,"parent_pid":21741,"available":false,"lou":"678","feature":true,"pub":true,"require":null,"option4":""},
{"id":1351572329731,"parent_pid":21741,"available":false,"lou":"678","feature":true,"pub":true,"require":null,"option4":""},
{"id":1351572943231,"parent_pid":21741,"available":true,"lou":"678","feature":true,"pub":true,"require":null,"option4":""},
{"id":1651572973431,"parent_pid":21741,"available":true,"lou":"678","feature":true,"pub":true,"require":null,"option4":""},

因此最终结果需要是 ['1651572973431','1351572943231']

感谢大大的帮助谢谢

最佳答案

这可能不是一个好的答案——这取决于你拥有什么。 看起来您有一个字符串列表,并且您想要其中一些字符串的 id。如果是这样的话,如果您解析 JSON 而不是编写拜占庭式正则表达式,那么它会更清晰且更易于阅读。例如:

import json

# lines is a list of strings:

lines = ['{"id":1351572979731,"parent_pid":21741,"available":false,"lou":"678","feature":true,"pub":true,"require":null,"option4":""}',
'{"id":1351572329731,"parent_pid":21741,"available":false,"lou":"678","feature":true,"pub":true,"require":null,"option4":""}',
'{"id":1351572943231,"parent_pid":21741,"available":true,"lou":"678","feature":true,"pub":true,"require":null,"option4":""}',
'{"id":1651572973431,"parent_pid":21741,"available":true,"lou":"678","feature":true,"pub":true,"require":null,"option4":""}',
]

# parse it and you can use regular python to get what you want:
[line['id'] for line in map(json.loads, lines) if line['available']]

结果

[1351572943231, 1651572973431]

如果你发的代码是一长串,你可以把它包在[]中,然后把它解析成一个数组,结果是一样的:

import json

line = r'{"id":1351572979731,"parent_pid":21741,"available":false,"lou":"678","feature":true,"pub":true,"require":null,"option4":""}, {"id":1351572329731,"parent_pid":21741,"available":false,"lou":"678","feature":true,"pub":true,"require":null,"option4":""}, {"id":1351572943231,"parent_pid":21741,"available":true,"lou":"678","feature":true,"pub":true,"require":null,"option4":""},{"id":1651572973431,"parent_pid":21741,"available":true,"lou":"678","feature":true,"pub":true,"require":null,"option4":""}'

lines = json.loads('[' + line + ']')
[line['id'] for line in lines if line['available']]

关于python - 匹配具有特定字符串的行以提取值 Python Regex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56229528/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com