1 回答
![?](http://img1.sycdn.imooc.com/545867790001599f02200220-100-100.jpg)
TA贡献1793条经验 获得超6个赞
请尝试下面的程序。该程序首先读取filea并创建样式和线条的字典,然后逐行读取fileb以从字典中匹配和选择样式,并将其写入到filec.
import re
table={}
with open("filea.txt","r") as f:
for line in f:
if line.strip():
parts=re.findall("^(\[.*?\])?(.*)$",line)[0]
if parts[0] in table:
table[parts[0]]+=parts[1]
else:
table[parts[0]]=parts[1]
with open("fileb.txt","r") as f, open("filec.txt","w") as f1:
for line in f:
if line.strip():
for i in table:
if line.strip() in table[i]:
f1.write(i+line)
break
else:
pass
else:
f1.write(line)
输出
[Base Font : PSJEPX+Muli-Light, Font Size : 7.5, Font Weight : 300.0]We are not satisfied with our 2018 results. We have the global footprint, assets and team to perform better. We have made a number of changes to position for sustainable growth.
New line that does not start with square brackets.
[Base Font : SVTVFR+Muli-SemiBold, Font Size : 8.1, Font Weight : 600.0]Innovation
添加回答
举报