更新时间:2023-01-30 20:00:02
您更正的模式是 HERE
我会将这样的文本分成块,然后在每个块中找到匹配项:
I would break text like that into BLOCKS and then find the matches in each block:
import csv
import re
fieldnames = ['AREA', 'MCC', 'MNC']
re_fields = re.compile(r'({})\s*=\s*([^,]+),'.format('|'.join(fieldnames)))
with open(fn) as f_input:
data=f_input.read()
for block in re.finditer(r'(?<=ADD IUACCAREALST:)(.*?)(?=ADD IUACCAREALST:|\Z)', data, flags=re.S | re.M):
print(re_fields.findall(block.group(1)))
打印:
[('AREA', 'RA'), ('MCC', '"510"'), ('MNC', '"28"')]
[('AREA', 'RA'), ('MCC', '"510"'), ('MNC', '"28"')]
此时,使用每个元组列表创建一个形成该 csv 记录的字典;将其写入 csv 文件.大功告成!
At that point, use each list of tuples to create a dict forming that csv record; write it to the csv file. Done!