f = open('Pajanimals.txt','rt')
lines = f.readlines()
f.close()
f = open('Pajanimals.txt','wt')
f.writelines([line.replace('+44','0') for line in lines])
f.close()
path = 'Pajanimals.txt'
try:
with open(path, 'r') as infile:
data = infile.read().replace('+44', '0')
except OSError as exception:
print('ERROR: could not read file:')
print(' %s' % exception)
else:
with open(path, 'w') as outfile:
outfile.write(data)
试试这个:
您只需在此处使用string.replace:
如果您要执行更复杂的操作,我可能会建议使用正则表达式,但您的情况非常简单。在
这里唯一要注意的是replace将所有它找到的实例,因此},但是有一个maxreplace参数可以用于将其限制为第一个实例:
^{pr2}$"+44123+44"
将变成{将
"+44123+44"
转换为"0123+44"
检查每一行没有意义-只需一次性替换所有内容:
相关问题 更多 >
编程相关推荐