说我有一个看起来像这样的输入文件(temp.tmpl):
PTF @
ARB @ C @ @ A @ @ C @
OSN @ B @ @ A @
SDA @ B @
CPN 3.23
SNL 3.26
并在其他文件中(candidate.txt):
A 3.323 B 4.325 C 6.32 D 723 E 8 F 9 G 1.782
H 7
I 4
J 9
K 10
我想用分配的值替换A,B和C.
完成我的任务所需的方法是找到
变量A,B和C,通过查找@ @ …然后知道这显然是一个变量.
然后更换它们.这是我尝试过的:
reader = open('candidate.txt', 'r')
out = open('output.txt', 'w')
dictionary = dict()
for line in reader.readlines():
pairs = line.split()
for variable, value in zip(pairs[::2],pairs[1::2]):
dictionary[variable] = value
#Now to open the template file
template = open('temp.tmpl', 'r')
for line1 in template:
if line1[1]:
confirm = line1.split(' ')[0].lower()
symbol = line1.split(' ')[1]
if confirm == 'ptf':
next(template)
elif symbol in line1:
start = line1.find(symbol)+len(symbol)
end = line1[start:].find(symbol)
variable = line1[start:start + end].strip()
print variable
而且我似乎无法弄清楚如何处理具有多组变量的行.
提前非常感谢您.
解决方法:
使用稀土吗?问题已更改,这是我修改后的解决方案:
import re
# Create translation dictionary
codes = re.split(r'\s',open('candidate.txt').read())
trans = dict(zip(codes[::2], codes[1::2]))
outfh = open('out.txt','w')
infh = open('data.txt')
# First line contains the symbol, but has a trailing space!
symbol = re.sub(r'PTF (.).*',r'\1', infh.readline()[:-1])
for line in infh:
line = re.sub('\\'+ symbol + r' ([ABC]) ' + '\\' + symbol,
lambda m: '%s %s %s' % (symbol,trans[m.groups()[0]],symbol),
line)
outfh.write(line)
outfh.close()
使用两个zip的dict是从[key,value,key,value,…]列表中创建字典的技巧
trans是具有名称及其相应值的字典.
r’@([ABC])@’捕获@符号内的A或B或C
lambda函数传递了一个match对象,在该对象上我们调用groups()方法.这将返回匹配括号组的元组,在这种情况下为A或B或C.我们将其用作字典trans的键,因此将其替换为值.