Find what you want in brackets in Python text format

Asked 2 years ago, Updated 2 years ago, 65 views

Hi, how are you?

In the text file contents, the data is recorded in the following format:

For example, if you want to find only the flawdata * {*} in the data below and import it in the form of a list, it's effective What's the way?

['A' , '1432'], ['B' , '2433']


flawdata A {
        mark 10 120
        drc 2 2 3 30 00
        id 1432
}
flawdata B {
        mark 10 220
        drc 1 2 3 33 30
        id 2433
}


python brace text

2022-09-22 18:18

1 Answers

Regular expressions look good.

import re

text = """flawdata A {
        mark 10 120
        drc 2 2 3 30 00
        id 1432
}"""

p = re.compile(r'flawdata (.+) \{(?:.|\n)*id (\d+)*(?:.|\n)*\}')
m = p.search(text)
print(m.groups()) # ('A', '1432')


2022-09-22 18:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.