當前位置:成語大全網 - 新華字典 - python 怎麽樣把文本a 的內容對文本b的內容進行匹配

python 怎麽樣把文本a 的內容對文本b的內容進行匹配

import re

from itertools import imap, ifilter

# 定義解析"b.txt"文件的正則表達式

patt = re.compile(r"""(?P<category>\S*)\s*(?P<amount>\d+)""")

# 初始化壹個計數器

counter = {}

with open("b.txt", "rt") as handle:

# 用正則表達式逐行解析"b.txt"

for m in ifilter(None, imap(patt.match, handle)):

d = m.groupdict()

# 更新計數器

counter[d["category"]] = counter.get(d["category"], 0) + int(d["amount"])